[Qt-interest] QTimer still fires after stop() AND delete

Thiago Macieira thiago at kde.org
Sat Oct 17 09:37:10 CEST 2009


Em Sábado 17. Outubro 2009, às 01.51.56, marc at mferland.net escreveu:
> Quoting Josiah Bryan <jbryan at productiveconcepts.com>:
> > Hi All -
> >
> > I think I'm going crazy, but this really seems to be what is happening:
> >
> > I've got a QTimer that I set to trigger changing of a widget label. The
> > user can also trigger the widget label using a button in the form. If
> > the user changes the label before the timer times out, then my code
> > calls the stop() method on the timer. However, the slot that is connect
> > to the timer is STILL CALLED even after stop is called.
> >
> > Okay, I thought it was just my mind playing tricks. So, instead of just
> > calling stop(), I stop(); disconnect(timer,0,this,0); delete timer;
> > timer=0; ... and the slot connect to the timer is STILL called.
> >
> > I know its the timer because the slot being called is ONLY connected to
> > that timer. Additionally, the gdb stack dump reports Qtimer::timerEvent
> > and QTimer::timeout in the stack that calls the slot.
> >
> > How can that possibly be? Why is the slot being called even though I've
> > stopped the timer?
> 
> This happened to me once, a thread was posting signals to another
> thread and sometimes after having stopped and deleted the thread, a
> signal could still come in from this "ghost" thread object.
> 
> You could maybe use an internal variable and check whether or not the
> slot has to process the events.
> 
> I think that Qt keeps the event objects inside the event list even
> though the sender has been destroyed.

That is correct.

Events, once posted, are tied to the receiver. It doesn't matter if the sender 
was deleted or not: the event is already posted and will be delivered. In 
fact, there's no way to determine the sender object with events -- there is no 
sender object for events.

That's also the reason why QObject::sender() returns 0 on queued connections.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358

Qt Developer Days 2009 | Registration Now Open!
Munich, Germany: Oct 12 - 14     San Francisco, California: Nov 2 - 4
      http://qt.nokia.com/qtdevdays2009
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091017/d0490762/attachment.bin 


More information about the Qt-interest-old mailing list