Results 1 to 5 of 5

Thread: Loop inside Qthread causes "QThread: Destroyed while thread is still running"?

  1. #1
    Join Date
    Jun 2008
    Posts
    13
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Loop inside Qthread causes "QThread: Destroyed while thread is still running"?

    Hi, I am new to C++ and Qt and despite that i am thinking of using threads in my app. Here is an example code which outputs "." and then "QThread: Destroyed while thread is still running" error after pressing button.

    Qt Code:
    1. class MyThread : public QThread
    2. {
    3. public:
    4. void run();
    5. };
    6. void MyThread::run()
    7. {
    8. int connected=0;
    9. while (connected==0)
    10. {
    11. fprintf(stderr,".");
    12. //Do something that modifies num when usb device is connected
    13. sleep(5);
    14. }
    15. }
    16. void MainWindowImpl::on_button_clicked()
    17. {
    18. MyThread t;
    19. t.start();
    20. }
    To copy to clipboard, switch view to plain text mode 
    0
    What can be wrong in this code? Am I not allowed to use while loop in thread?
    Last edited by zolookas; 26th June 2008 at 18:07.

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Loop inside Qthread causes "QThread: Destroyed while thread is still running"?

    What are you doing this for? You just make your thread hang.
    I'm a rebel in the S.D.G.

  3. #3
    Join Date
    Jun 2008
    Posts
    13
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Loop inside Qthread causes "QThread: Destroyed while thread is still running"?

    I am doing different things, but i don't want to post a bunch of unrelated code to confuse others. I've stripped everything that is unnecessary before posting.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Loop inside Qthread causes "QThread: Destroyed while thread is still running"?

    "MyThread t" goes out of scope and gets destructed according to normal C++ rules immediately in the end of MainWindowImpl::on_button_clicked(). You might want to allocate it on the heap instead.
    Last edited by jpn; 26th June 2008 at 20:03. Reason: disabled smilies
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    zolookas (29th July 2008)

  6. #5
    Join Date
    Jun 2008
    Posts
    13
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Loop inside Qthread causes "QThread: Destroyed while thread is still running"?

    Quote Originally Posted by jpn View Post
    "MyThread t" goes out of scope and gets destructed according to normal C++ rules immediately in the end of MainWindowImpl::on_button_clicked(). You might want to allocate it on the heap instead.
    Thanks, it worked.

    If anybody wants to now exactly how i did it: i've replaced:
    Qt Code:
    1. MyThread t;
    2. t.start();
    To copy to clipboard, switch view to plain text mode 
    with
    Qt Code:
    1. MyThread *t=new MyThread;
    2. t->start();
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 26th June 2008 at 20:03. Reason: disabled smilies

Similar Threads

  1. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13
  2. QThread: Destroyed while thread is still running
    By Shuchi Agrawal in forum Newbie
    Replies: 8
    Last Post: 3rd April 2007, 07:27
  3. Workload in a QThread blocks main application's event loop ?
    By 0xBulbizarre in forum Qt Programming
    Replies: 14
    Last Post: 9th April 2006, 22:55

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.