Results 1 to 20 of 33

Thread: QObject::killTimers() warning after timer stopped from its own thread

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android
    Thanks
    7
    Thanked 3 Times in 2 Posts

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    Quote Originally Posted by wysota View Post
    Hmmm.... whaaaat?
    Please read the thread to help with confusion. I don't understand the quip. I was replying to someone else's comment.

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

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    I had read the thread before posting. It didn't help with my confusion

    So is your problem fixed or not?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    Quote Originally Posted by astodolski View Post
    Ok. I made the illustration (with code) as to the point I was making - less the semantics. The change in use of the timer changes the outcome. I did this change from referencing the article which points to proper use of threads. The other prior suggestions didn't affect the outcome of the message warning.
    Unfortunately your semantics (talking about references) are plain wrong and need to be corrected if you want to continue talking in c++ parlance.

    re bolded sentence: Well it will, or it wont, or it might. It all depends on whether you correctly pass parent pointer to the qtimer and whether there is truly a Qt bug here - you still haven't clarified your change (with code or without) - less or including semantics.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

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

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    Guys, but where do you see a Qt bug here?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android
    Thanks
    7
    Thanked 3 Times in 2 Posts

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    Quote Originally Posted by wysota View Post
    Guys, but where do you see a Qt bug here?
    This comment looks like the right question. I don't know if it's a bug. I was working on the suggestions of things to try. I then added a change on my own. This then devolved into trivial banter of what to call a parameter passed to connect. I just want to get the issue resolved as there seems to be lots of discussion on threading in general. I posted the change in the call to connect with a code snippet. It shows a different result. Is it a bug? I don't know. Did I sin in misquoting c++ scripture? Perhaps. All who have read this knows what was intended and I'm just trying to understand. The code posted in total I hope is enough (short of a complete app) to answer the relevant question as to whether the thread was moved, and/or whether the parent pointer was passed to the timer. This is shown at top code listing. Whether wrong or a bug IS the discussion.

    przejrzystość

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

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    In my opinion there is no bug here. If I'm right then sometimes you are not stopping the timer before the thread ends. Thus when at some point the object is destructed from within the main thread, the destructor of the timer calls stop() and since the object has a different thread affinity, you get the warning message. You either have to move the worker object back to the main thread before the worker thread ends or you have to make sure the timer is stopped before the thread ends. You'd need some "aboutToFinish" signal that you can use to stop the timer.

    For instance this line of your code is surely invalid:

    Qt Code:
    1. connect(workerThread, SIGNAL(finished()), worker, SLOT(stop()));
    To copy to clipboard, switch view to plain text mode 

    workerThread and worker live in different threads (wT in main thread, w in worker thread) thus the connection is going to be queued and effectively stop() will likely never be called because it'd require workerThread's event loop to be running (which it isn't because the thread has just finished() (after exiting QThread::run()).

    I suggest to sit down with a piece of paper, a pencil and draw some kind of graph or list of states and transitions that need to happen during the lifetime of the thread and the worker. Pay special attention to thread affinity so that you are 100% sure when a particular slot is going to be called. Then implement your state machine. It might be easiest to just push the object back to the original thread before the worker thread dies, e.g. by doing the following:

    Qt Code:
    1. void MyThread::run() {
    2. exec();
    3. if(worker)
    4. worker->moveToThread(QCoreApplication::instance()->thread());
    5. }
    To copy to clipboard, switch view to plain text mode 

    Another possibility would be to connect to the finished() signal with Qt::DirectConnection to do some cleanup before the thread dies assuming that the signal is emitted in the context of the worker thread. If not, subclass QThread and make run() emit some signal (e.g. aboutToFinish()) before it returns.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android
    Thanks
    7
    Thanked 3 Times in 2 Posts

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    Quote Originally Posted by wysota View Post
    In my opinion there is no bug here. If I'm right then sometimes you are not stopping the timer before the thread ends.
    That was related to a previous question I WAS going to attempt to answer

    Thus when at some point the object is destructed from within the main thread, the destructor of the timer calls stop() and since the object has a different thread affinity, you get the warning message. You either have to move the worker object back to the main thread before the worker thread ends or you have to make sure the timer is stopped before the thread ends. You'd need some "aboutToFinish" signal that you can use to stop the timer.
    aboutToFinish would be more convenient.


    It might be easiest to just push the object back to the original thread before the worker thread dies, e.g. by doing the following:

    Qt Code:
    1. void MyThread::run() {
    2. exec();
    3. if(worker)
    4. worker->moveToThread(QCoreApplication::instance()->thread());
    5. }
    To copy to clipboard, switch view to plain text mode 
    This approach then suggests the use of subclassing QThread doesn't it?

    Another possibility would be to connect to the finished() signal with Qt:irectConnection to do some cleanup before the thread dies assuming that the signal is emitted in the context of the worker thread. If not, subclass QThread and make run() emit some signal (e.g. aboutToFinish()) before it returns.
    Another reason to continue use of subclass of QThread.
    Thank you.

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

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    Quote Originally Posted by astodolski View Post
    This approach then suggests the use of subclassing QThread doesn't it?
    Yes. There is nothing wrong with subclassing QThread. It gets to be wrong when you start adding slots to the subclass

    However you don't have to subclass QThread at all. You can emit the signal from the Worker object as well when it's done its work or just immediately move it to the main thread. It breaks some OOP concepts but it's still a valid approach.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    Quote Originally Posted by amleto View Post
    ok, next try. Does this give same thread timer warning?

    Qt Code:
    1. FlashWizard::FlashWizard(...)
    2. {
    3. ...
    4. worker = new Worker;
    5.  
    6. qDebug() << worker->thread();
    7.  
    8. workerThread = new QThread;
    9. worker->moveToThread(workerThread);
    10.  
    11. qDebug() << worker->thread();
    12. qDebug() << worker->m_workerTimer->thread();
    13.  
    14. //connect(workerThread, SIGNAL(started()), worker, SLOT(start()));
    15. connect(workerThread, SIGNAL(finished()), worker, SLOT(stop()));
    16. connect(worker, SIGNAL(finished()), workerThread, SLOT(quit()));
    17. connect(worker, SIGNAL(finished()), ui->programPage, SLOT(setDone()));
    18. connect(worker, SIGNAL(updateProgress(int)), this, SLOT(updateWritingProgressBar(int)));
    19.  
    20. workerThread->start();
    21. QMetaObject::invokeMethod(worker, "start");
    22. QMetaObject::invokeMethod(worker, "stop");
    23. }
    To copy to clipboard, switch view to plain text mode 
    Quote Originally Posted by astodolski View Post
    Yes the same message. <SNIP>
    Quote Originally Posted by wysota View Post
    Guys, but where do you see a Qt bug here?
    That is a bug if that truly happened. But it seems the OP is not being honest/accurate.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

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

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    This works fine for me on Qt 4.8 and 5.0 RC2. I'm not getting any warnings.

    Qt Code:
    1. include <QtCore>
    2.  
    3. class Worker : public QObject {
    4. Q_OBJECT
    5. public:
    6. Worker() : QObject(){
    7. timer = new QTimer(this);
    8. timer->setInterval(1000);
    9. }
    10. public slots:
    11. void start() { qDebug() << Q_FUNC_INFO; timer->start(); }
    12. void stop() { qDebug() << Q_FUNC_INFO; timer->stop(); }
    13. private:
    14. QTimer *timer;
    15. };
    16.  
    17. #include "main.moc"
    18.  
    19. int main(int argc, char **argv) {
    20. QCoreApplication app(argc, argv);
    21. QThread thread;
    22. Worker *worker = new Worker;
    23. worker->moveToThread(&thread);
    24. thread.start();
    25. QMetaObject::invokeMethod(worker, "start");
    26. QMetaObject::invokeMethod(worker, "stop");
    27. QObject::connect(&app, SIGNAL(aboutToQuit()), &thread, SLOT(quit()));
    28. QTimer t;
    29. t.start(1000);
    30. QObject::connect(&t, SIGNAL(timeout()), &app, SLOT(quit()));
    31. app.exec();
    32. thread.wait();
    33. }
    To copy to clipboard, switch view to plain text mode 

    You can build on this example to check your actual code (with timers and stuff).

    Edit: I even added the timer stuff to the code, still works as expected.
    Last edited by wysota; 18th December 2012 at 02:03.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android
    Thanks
    7
    Thanked 3 Times in 2 Posts

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    I've compiled a complete application with the changes that have been posted in this thread. It's possible as you have suggested that the app is attempting to stop the timer before the thread completes.

    test app attached here ->> workerTest.zip

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

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    Does the code you posted still contain the bug? I'm not getting any warnings.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android
    Thanks
    7
    Thanked 3 Times in 2 Posts

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    Quote Originally Posted by wysota View Post
    Does the code you posted still contain the bug? I'm not getting any warnings.
    The code example I posted still reports the warning message. Are you able to compile and run it? I am using 4.8.4 with Win SDK 7.1 compiler under Creator 2.6

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

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    I'm not getting any warnings. I use Qt 4.8.3 x64 Linux. Same output with Qt 5.0 RC2.

    $ ./FlashWizard
    Main thread: QThread(0xcde710)
    Worker Object Thread Id: QThread(0x7ffff75eff00)
    Now using thread: QThread(0x7ffff75eff00)
    1
    2
    3
    ...
    97
    98
    99
    100
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #15
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android
    Thanks
    7
    Thanked 3 Times in 2 Posts

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    Quote Originally Posted by wysota View Post
    I'm not getting any warnings. I use Qt 4.8.3 x64 Linux. Same output with Qt 5.0 RC2.
    Oh I guess the problem wasn't fully realized. It's only when you click the cancel button before the loop finishes that the message is shown - sorry. I should have re-iterated that. I though that was evident within the thread.

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

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    Still, no warning

    An idea: maybe you shouldn't be using a thread at all? What do you need it for?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. #17
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android
    Thanks
    7
    Thanked 3 Times in 2 Posts

    Default Re: QObject::killTimers() warning after timer stopped from its own thread

    Quote Originally Posted by wysota View Post
    Still, no warning

    An idea: maybe you shouldn't be using a thread at all? What do you need it for?
    So let me understand. You run this app. Before the count gets to 100, hit the brakes with the Cancel button. You're telling me no message like "QObject::killTimer: timers cannot be stopped from another thread"?

    This is a stripped out app that is used as a prototype for use with a hardware DLL. That library has functions that are called for each timer tick and have to perform tasks in the background that would otherwise tie up the UI. I created this to perform a task (a loop counter in this case) and update a progress bar. I find it incredible that this test app loaded on a host PC and on a virtual machine and on my own laptop all perform exactly the same - that is, the killTimer warning is displayed in the debug window after the Cancel button is clicked and before the counter gets to 100, except for you.

    I read your article on keeping the UI responsive as well as general considerations when making calls to hardware while updating a UI with background workers. I feel that background tasks belong in another thread so as to keep the UI responsive. That's my reason for this prototype. That's pretty much boilerplate practice. I don't have a Linux distro prepared for testing on there.


Similar Threads

  1. Replies: 1
    Last Post: 29th July 2010, 05:41
  2. QObject::killTimer Warning Messages
    By mclark in forum Qt Programming
    Replies: 7
    Last Post: 11th September 2008, 20:10
  3. how to enable a timer in a non-gui thread?
    By zeopha in forum Qt Programming
    Replies: 3
    Last Post: 5th August 2008, 09:29
  4. Replies: 2
    Last Post: 24th March 2008, 16:59
  5. QObject::moveToThread warning
    By mnemonic_fx in forum Qt Programming
    Replies: 3
    Last Post: 10th August 2007, 22:11

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
  •  
Qt is a trademark of The Qt Company.