[Qt-interest] Guarded pointer safety

Andreas Pakulat apaku at gmx.de
Fri Aug 14 22:23:04 CEST 2009


On 14.08.09 22:01:29, Imre Péntek wrote:
> Hello!
> 
> 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.
> 
> So, as I imagine the scenario:
> 1. for some reason there is two signals in the queue
> 2. the slot connected to the first one deletes that object, so the signal that 
> would act upon the deletion would be served third
> 3. the slot connected to the second one uses that pointer: if(p) segfault.
> 4. if we wouldn't crashed already, that p would be set to zero here

This doesn't happen in single-thread apps usually, because there
signal/slot connections are direct, hence after the first slot deletes
the qobject, its destroyed signal automatically calls any connected
slots directly, before returning control to the first slot.

In a multithreaded app, when the destryoed-signal connection is direct,
then yes you'd get into trouble. However for QObjects you should usually
use deleteLater instead of direct delete inside a slot.

Andreas

-- 
You're working under a slight handicap.  You happen to be human.



More information about the Qt-interest-old mailing list