Results 1 to 8 of 8

Thread: QObject::killTimer Warning Messages

  1. #1
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question QObject::killTimer Warning Messages

    I've noticed the following two warning messages in my debug output window when my app exits. I'm running Qt 4.3.4 under Visual Studio 2005.

    QObject::killTimer: timers cannot be stopped from another thread
    QObject::killTimers: timers cannot be stopped from another thread
    I am using 2 QTimers in my app. One is a private member in a QThread (where I believe the warnings are coming from). The other is a private member of my QMainWindow, but the way I'm using the app, the timer shouldn't be created (it should only be created when running the app from the command-line).

    Can anyone give me some insight as to what could be causing these messages?

    Qt Code:
    1. ////////////////////////////////////////////////////////////////////////////////
    2. // In the constructor of the QMainWIndow
    3. m_pPropTimer = NULL;
    4.  
    5. m_pPropTimer = new QTimer( this );
    6. connect( m_pPropTimer, SIGNAL( timeout() ), this, SLOT( nonGUIReadTimeout() ) );
    7.  
    8. // In the destructor of the QMainWIndow
    9. if ( (m_pPropTimer != NULL) && m_pPropTimer->isActive() )
    10. m_pPropTimer->stop();
    11. ////////////////////////////////////////////////////////////////////////////////
    12.  
    13. // This class is what I believe is causing the messages
    14. WatchDogThread::WatchDogThread( QObject* parent ) : QThread( parent )
    15. {
    16. m_parent = static_cast<CController*>( parent );
    17. m_pTimer = NULL;
    18. }
    19.  
    20. WatchDogThread::~WatchDogThread( void )
    21. {
    22. if ( m_pTimer != NULL )
    23. {
    24. if ( m_pTimer->isActive() )
    25. m_pTimer->stop();
    26.  
    27. delete m_pTimer;
    28. }
    29. }
    30.  
    31. void WatchDogThread::run( void )
    32. {
    33. m_pTimer = new QTimer();
    34. connect( m_pTimer, SIGNAL( timeout() ), this, SLOT( checkTimestamp() ) );
    35. m_pTimer->start( 5000 );
    36.  
    37. exec();
    38. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QObject::killTimer Warning Messages

    The thread object doesn't live in the thread it creates thus the destructor runs in the main thread and not your worker thread. Move the contents of your destructor into the run() method right after exec().

  3. #3
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject::killTimer Warning Messages

    Thanks for the reply. I placed the timer deletion code in the run() method but after exec() returns, the m_pTimer member value indicates that the QTimer instance has been deleted. The end result, of course, is a crash in QTimer because the value is bad.
    1. What\Who is deleting the QTimer?
    2. Why is the m_pTimer value valid immediately after exec() returns but invalid when isActive() is called?
    3. Would it be better to handle the deletion of m_pTimer in response to the QThread::finished() signal?

    m_pTimer BEFORE exec() 0x1d8fc48
    m_pTimer AFTER exec() 0x1d8fc48
    m_pTimer BEFORE isActive() 0xfeeefeee
    Qt Code:
    1. void WatchDogThread::run( void )
    2. {
    3. m_pTimer = new QTimer();
    4. connect( m_pTimer, SIGNAL( timeout() ), this, SLOT( checkTimestamp() ) );
    5. m_pTimer->start( 5000 );
    6.  
    7. qDebug( "m_pTimer BEFORE exec() 0x%x", m_pTimer );
    8. exec();
    9. qDebug( "m_pTimer AFTER exec() 0x%x", m_pTimer );
    10.  
    11. if ( m_pTimer != NULL )
    12. {
    13. qDebug( "m_pTimer BEFORE isActive() 0x%x", m_pTimer );
    14. if ( m_pTimer->isActive() ) //<-- CRASH
    15. m_pTimer->stop();
    16.  
    17. delete m_pTimer;
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QObject::killTimer Warning Messages

    Quote Originally Posted by mclark View Post
    What\Who is deleting the QTimer?
    Provided that the timer doesn't have a parent it has to be you or the timer is not deleted at all.
    Why is the m_pTimer value valid immediately after exec() returns but invalid when isActive() is called?
    Maybe you have a local variable somewhere that is named the same and it shadows the other variable?

    Would it be better to handle the deletion of m_pTimer in response to the QThread::finished() signal?
    No. If you create an object in run(), destroy it there. The timer won't timeout anyway after you return from exec() so there is no point in keeping it. I even suggest you change your run() a bit:

    Qt Code:
    1. void ...:run(){
    2. QTimer timer;
    3. connect(&timer, SIGNAL(timeout()), this, SLOT(checkTimestamp()));
    4. timer.start(5000);
    5. exec();
    6. // the timer will get deleted when run() returns
    7. }
    To copy to clipboard, switch view to plain text mode 

    By the way, I hope you realize the slot will be called in the context of the main thread and not the worker thread. What is the point in doing all that in a worker thread anyway? Does the thread do anything else besides running the timer?

  5. #5
    Join Date
    May 2008
    Posts
    58
    Thanks
    2

    Default Re: QObject::killTimer Warning Messages

    I think you should do that in :
    QThread::finished()

  6. #6
    Join Date
    May 2008
    Posts
    58
    Thanks
    2

    Default Re: QObject::killTimer Warning Messages

    Hi Mclark,I have tested your code, every thing is right on my PC,
    I am using Qt4.3 with Visual Studio 2005
    my code:
    -----.h file---------------
    Qt Code:
    1. class CMyThread : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. void run();
    6.  
    7. private:
    8. QTimer* mTimer;
    9. };
    To copy to clipboard, switch view to plain text mode 

    ----.cpp file----------------
    Qt Code:
    1. void CMyThread :: run()
    2. {
    3. mTimer = new QTimer();
    4. mTimer->start(1000);
    5. exec();
    6.  
    7. if ( mTimer )
    8. {
    9. if ( mTimer->isActive() )
    10. {
    11. mTimer->stop();
    12. }
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    in main function
    -------------------------------
    CMyThread mythread;
    mythread.start();
    ......
    mythread.exit();
    Last edited by jacek; 11th September 2008 at 18:18. Reason: missing [code] tags

  7. #7
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject::killTimer Warning Messages

    Quote Originally Posted by wysota View Post
    Provided that the timer doesn't have a parent it has to be you or the timer is not deleted at all.
    Even when I remove the 'delete m_pTimer' code the m_pTimer value changes after returning from exec(). It gets the 0xfeeefeee value. There are no other m_pTimer variables declared anywhere in this class.

    Quote Originally Posted by wysota View Post
    By the way, I hope you realize the slot will be called in the context of the main thread and not the worker thread. What is the point in doing all that in a worker thread anyway? Does the thread do anything else besides running the timer?
    The purpose of this thread is to:
    1. Maintain a list of objects. When the object is created it is put into the list. When the object is fully initialized (due to outside forces) it is removed from the list.
    2. Run the timer. When the timer times out, the list is checked. If it contains any objects that have been waiting to be initialized for too long, they are rescheduled and removed from the list. At this point the timer is restarted.

    I admit I am a novice when it comes to threading, but I don't see why the slot is run in the main threads context instead of the worker thread. The 'this' pointer in the connect() call is the worker thread pointer, no?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QObject::killTimer Warning Messages

    Quote Originally Posted by mclark View Post
    The purpose of this thread is to:
    1. Maintain a list of objects. When the object is created it is put into the list. When the object is fully initialized (due to outside forces) it is removed from the list.
    2. Run the timer. When the timer times out, the list is checked. If it contains any objects that have been waiting to be initialized for too long, they are rescheduled and removed from the list. At this point the timer is restarted.
    You don't need a separate thread for that.

    I admit I am a novice when it comes to threading, but I don't see why the slot is run in the main threads context instead of the worker thread. The 'this' pointer in the connect() call is the worker thread pointer, no?
    No, it is a pointer to an object representing the thread. And the object itself lives in the context of the thread that created it (the object, not the thread) therefore it lives in context of the main thread.

  9. The following user says thank you to wysota for this useful post:

    mclark (11th September 2008)

Similar Threads

  1. Visual Studio 2005 Express
    By Muzz in forum Installation and Deployment
    Replies: 22
    Last Post: 6th November 2008, 06:21
  2. How to Compile VTKDesigner2 with Qt?
    By alfredoaal in forum Newbie
    Replies: 0
    Last Post: 5th September 2008, 05:34
  3. Crash handler on Win32
    By niko in forum Qt Programming
    Replies: 3
    Last Post: 12th November 2007, 19:41
  4. Problem at time compilation in traslation of language
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2007, 14:18
  5. how to corss compile for windows in Linux
    By safknw in forum Qt Programming
    Replies: 24
    Last Post: 13th May 2006, 05:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.