[Interest] more QSharedPointerQuestions

André Pönitz andre.poenitz at mathematik.tu-chemnitz.de
Thu Mar 1 23:16:25 CET 2012

On Thu, Mar 01, 2012 at 01:34:04PM -0800, Jason H wrote:
> So I have a QObject-derived object tree, like a QObject* tree; I made
> everything QSharedObject<QObject> accordingly.


QObject ownership is typically handled easily using the parent-child
relationship. Give your objects a parent object and forget about their
individual lifetime. Manage only the parent object's lifetime.

> (I'll just use QObject in this email)
> I have a recursive query function. I want to use QSharedPointers so that I
> don't give the pointer directly to the tree because someone might delete it.

Who is "someone", and why may he delete an object that is passed for

> (ignoring data(), because at that point in time I can consider it
> intentional) 

A 'delete p;' is exactly as intentional as a 'delete p.data();'
A shared pointer does not increase safety in this case.

> However I hit a problem in my recursive search function
> QSharedObject<QObject> QObject::search() when I go to add "this" item
> to the result set, I can only add "this" as a QObject* not
> QSharedPointer<this>.
> How do I work around "this"?

Establish a clear concept of ownership for your objects and make sure
the query function sticks to it, i.e. does not delete things randomly.
I don't think you will still need shared pointers for the task afterwards.


More information about the Interest mailing list