[Interest] QGaphicsItem deletion
André Somers
andre at familiesomers.nl
Tue Sep 13 07:56:40 CEST 2016
On woensdag 15 juni 2016 19:00:34 CEST Igor Mironchik wrote:
> Hello.
>
> On 15.06.2016 16:59, Konstantin Shegunov wrote:
> > Hello,
> >
> >
> > On Wed, Jun 15, 2016 at 4:15 PM, Igor Mironchik
> >
> > <igor.mironchik at gmail.com <mailto:igor.mironchik at gmail.com>> wrote:
> > Parent of the handles is the same as Line's. But Line own objects
> > of handles with QScopedPointer.
> >
> > This doesn't sound right. If the handles are not children to the line,
> > then the line doesn't own them. Leaving semantics aside, it sounds
> > you're trying to "hijack" the ownership from the parent by using these
> > scoped pointers.
>
> Exactly. I need to Line be as parent of handles but they should be
> parented to the scene (read form in my app) because I need
> boundingRect() to return exactly bounding rectangle of the line without
> handles. An I need handles to be removed with the Line if Line deleted.
>
> I know that I can implement my own method in Line like
> boundingRectOfLine() and make handles children of Line. May be it would
> be a better solution, but at this time I have such code and it work...
>
> > So my question is it possible that handle will be deleted before
> > Line, so QScopedPointer will try to free already deleted pointer?
> >
> > From your description I'd guess this is exactly what's happening.
>
> Here you aren't right. In d_tor() of QGraphicsScene I see:
>
> while (!d->topLevelItems.isEmpty())
> delete d->topLevelItems.first();
>
> where topLevelItems are in creating order...
>
> and in QGraphicsItem d_tor()
>
> while (!d_ptr->children.isEmpty())
> delete d_ptr->children.first();
>
> So when Line deleted it places before handles in children list, and
> before Line deleted handles already deleted and removed from children list.
>
Fine. So in the current implementation, it may actually work. But be aware
that there is no guarantee whatsoever that it will always work or will keep
working in the future. I would not depend on an implementation detail like
that.
André
More information about the Interest
mailing list