New shared pointer

Ignacio Castaño castanyo at yahoo.es
Sat Sep 10 02:07:07 BST 2005


Thiago Macieira <thiago at kde.org> wrote:
> You're not supposed to do that. The solution is really simple: do NOT mix 
> dumb pointers with smart pointers.

I obviously know that you are not supposed to do that. But there's nothing
enforcing you not to do it. In fact, in my experience it's a very common
error.

The motivation of that kind of pointer is that you can use them with any type
and not only with types that were originally designed to by shared. The
problem is that in many cases those types will already be used by the
existing APIs and for that reason the situation that I proposed can easily
happen.

I think it's far easier to only update the pointers that have ownership
semantics, than to update *all* the pointers, whenever you want to use shared
ownership. Not to mention that in some cases mixing them will be unavoidable,
because changing the API would break the binary compatibility.

I'm not saying that the proposed design is broken, I just think that in my
experience it's error prone, and that the current solution is in my opinion
superior, although there's still room for improvement to make it more robust.


> However, the extra indirection is unavoidable unless your object has the 
> refcount itself and that's exactly what Frerich is trying to avoid.

It's not only an extra indirection, but also an extra allocation. That might
not be very important, but well, I have console background and those things
make me nervous.

IIRC boost::shared_ptr does not have the extra indirection, but shared
pointers actually contain two pointers, one to the reference holder and
onther to the actual objects. I think that's even worse, instead increasing
the size of the shared objects that increases the size of all the references!

I've uploaded my smart pointeres for your reference:

http://castano.ludicon.com/files/pi/Ptr.h
http://castano.ludicon.com/files/pi/RefCounted.h

The important classes are PiPtr and PiRefCounted. There's also a weak pointer
that you can find useful.

Those are based on the code and ideas of Charles Bloom and Thacher Ulrich,
two former colleages.

http://www.cbloom.com/3d/galaxy3/galaxy3.zip
http://cvs.sourceforge.net/viewcvs.py/tu-testbed/tu-testbed/base/smart_ptr.h?rev=1.9&view=auto

Hope you find that useful!

--
Ignacio Castaño
castanyo at yahoo.es



		
______________________________________________ 
Renovamos el Correo Yahoo! 
Nuevos servicios, más seguridad 
http://correo.yahoo.es




More information about the kde-core-devel mailing list