[Development] On QML, ownership, QObject-trees and QSharedPointer

Olivier Goffart olivier at woboq.com
Thu May 24 16:26:19 CEST 2012

On Thursday 24 May 2012 11:46:05 Alberto Mardegan wrote:
> On 05/23/2012 02:43 PM, Thiago Macieira wrote:
> > My original solution was to have a parent only deref its children's shared
> > pointer counter, deleting it only if it became zero. That means that if
> > you
> > had a QSharedPointer to an object in the middle of the hierarchy, that
> > object would be orphaned instead of deleted when the parent died. The
> > converse was that when the last QSharedPointer reference to a QObject
> > went away, the object might survive if it still had a parent.
> Nice! However, a very hard thing to do as an aftertought. IMHO, it would
> be easier if the reference count was built in QObject itself, and
> QSharedPointer<QObject> would just play with that reference count.

Then it would be like a QExplicitlySharedDataPointer<>  (Not a really good 
name, but already does what you want)
And you can have that for any object if you have multiple inheritence with 
some QObject and QSharedData for example.

> Then you would also be able to make the "delete" operator work
> consistently with reference-counted QObjects, by overriding it and make
> it just decrement the count (and delete the object if the count is 0).

No. That's not how the delete operator works.
The operator delete is called after the destructors has already been run, and 
its purpose is just to release the memory.  the operator delete cannot decide 
not to delete.


Woboq - Qt services and support - http://woboq.com

More information about the Development mailing list