[Qt-interest] Guarded pointer safety

Andreas Pakulat apaku at gmx.de
Sat Aug 15 02:06:20 CEST 2009


On 14.08.09 22:48:04, Arnold Krille wrote:
> On Friday 14 August 2009 22:01:29 Imre Péntek wrote:
> > I've read the doc at http://doc.trolltech.com/qq/qq14-guardedpointers.html
> > , and I am still wondering whether it is safe to use QPointer even in a
> > single- threaded QT application. At the time I delete the object, it might
> > be a signal in the queue pending, with the appropriate slots using that
> > exact object.
> 
> If your app is single-thread only, there are no signals in the queue, because 
> then all signals are direct connections.
> But: There might still be events in the queue. I believe Qt uses autoguarded 
> pointers in the event loops queue. But to be on the save side, delete your 
> objects with deleteLater() so they get deleted via an event that is queue 
> after all the events currently queued for your object.

Right so far.

> That also works on multi-threaded apps where signals become events when 
> crossing threads.

No not really, because deleteLater will queue them in the eventloop
belonging to the receiver (the qobject to be deleted). So if you create
a QObject in thread 2, then send a signal to thread 1 which is connected
to two slots there and the first calls deleteLater, then it might happen
that the second slot will get a garbage pointer if thread 2 had an
eventloop running and hence deleted the object in the meantime.

At least if I read QCoreApplication::postEvent correctly.

Andreas

-- 
You will attract cultured and artistic people to your home.



More information about the Qt-interest-old mailing list