PDA

View Full Version : QSharedPointer - does it work well when constructed independently?



Septi
22nd July 2010, 13:29
Suppose there is one object in my program, and several shared pointers for it. Will they count references to the object, if they are not created with assigning value to one another, but by constructing each independently from simple pointer.

Like, this:

MyObject *object = new MyObject;
QSharedPointer <MyObject> a, b, c, d;
a = QSharedPointer (object);
b = QSharedPointer (object);
c = QSharedPointer (object);
d = QSharedPointer (object);
instead of this:

MyObject *object = new MyObject;
QSharedPointer <MyObject> a, b, c, d;
a = QSharedPointer (object);
b = a;
c = b;
d = c;

spud
22nd July 2010, 16:52
The first example won't work since the shared pointers have no knowledge of each other and object will get deleted four times over.

Septi
23rd July 2010, 03:41
Uh-oh, now I have to redesign my code a little. I thought they were counting references in some sort of static structure ((

Well, spud, thanks for clarifying it.