Results 1 to 2 of 2

Thread: QThread vs QThread* and QThread: Destroyed while thread is still running warning

  1. #1
    Join Date
    May 2010
    Location
    Rousse, Bulgaria
    Posts
    25
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QThread vs QThread* and QThread: Destroyed while thread is still running warning

    Greetings.
    I am trying to understand the concept behind using threads and have subclassed QThread the following way:

    Qt Code:
    1. // Header:
    2.  
    3. #include <QtDebug>
    4.  
    5. class TestThread : public QThread
    6. {
    7. public:
    8. void run();
    9. };
    10.  
    11. // C++ source:
    12.  
    13. void TestThread::run() {
    14. qDebug() << "Message from thread";
    15. exec();
    16. }
    17.  
    18. // main.cpp:
    19.  
    20. #include "testthread.h"
    21.  
    22. int main(int argc, char** argv) {
    23. TestThread t1, t2;
    24. t1.start();
    25. t2.start();
    26. qDebug() << "Message from main function";
    27. return 0;
    28. }
    To copy to clipboard, switch view to plain text mode 
    It returns 2 warnings QThread: Destroyed while thread is still running warning
    and then it crashes.
    However, if I replace
    Qt Code:
    1. TestThread t1,t2;
    To copy to clipboard, switch view to plain text mode 
    with
    Qt Code:
    1. TestThread *t1 = new TestThread, *t2 = new TestThread;
    To copy to clipboard, switch view to plain text mode 
    it works. My question is: why?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QThread vs QThread* and QThread: Destroyed while thread is still running warning

    This nothing specific about QThreads, but normal C/C++ behaviour.

    Case 1: In which you get warning, TestThread obejcts are created on stack, with local scope, compiler calls destructor at the end of main()
    Case 2: In which no warning, TestThread obejcts are created on heap (dynamic object), compiler does not explicitly destroy this, you have write code to manually delete these objects on heap. Objects not destroyed - so no warnning.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    Axtroz (4th October 2012)

Similar Threads

  1. How can I get the thread ID out of QThread
    By Artschi in forum Qt Programming
    Replies: 9
    Last Post: 8th November 2017, 04:27
  2. Replies: 11
    Last Post: 12th September 2012, 17:25
  3. QThread not running in different thread
    By lauwe in forum Qt Programming
    Replies: 2
    Last Post: 28th February 2011, 00:02
  4. Replies: 4
    Last Post: 26th June 2008, 19:41
  5. QThread: Destroyed while thread is still running
    By Shuchi Agrawal in forum Newbie
    Replies: 8
    Last Post: 3rd April 2007, 07:27

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.