Results 1 to 4 of 4

Thread: QThread::wait() causing crash

  1. #1
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QThread::wait() causing crash

    Greetings!

    I'm having a problem with my QThread classes. I based them on the Qt Example Mandlebrot (I'm also doing an long rendering process). I extracted the core mutex and waitCondition syncronization that was in the renderThread into it's own class (which I call InfiniteThread) and then I sub-class this to specialize what task that thread will perform.

    Problem is, whenever an instance of InfiniteThread is destroyed and the destructor gets called I get a SIGABRT and the following console output:

    pure virtual method called
    terminate called without an active exception
    The program has unexpectedly finished.
    Here's the top of the stack at the time:

    Qt Code:
    1. 0 __semwait_signal 0 0x00007fff87ecdeb6
    2. 1 _pthread_cond_wait 0 0x00007fff87ed1cd1
    3. 2 QWaitCondition::wait 0 0x000000010103bfa0
    4. 3 QThread::wait 0 0x00000001010a419e
    5. 4 InfiniteThread::~InfiniteThread infinitethread.cpp 17 0x000000010001022c
    6. 5 EMSampleThread::~EMSampleThread emsamplethread.cpp 18 0x000000010000fbfd
    7. 6 RFViewer::~RFViewer rfviewer.cpp 50 0x0000000100008a8f
    8. 7 main main.cpp 10 0x000000010000602a
    To copy to clipboard, switch view to plain text mode 

    So the SIGABRT seems to be coming from QThread::wait(), which is called in ~InfiniteThread(). Code for InfiniteThread is included below. Note that InfiniteThread::centralTask() is a pure-virtual member that is re-implemented to specialize the thread for a specific task.

    Like I mentioned before, InfiniteThread is based entirely on renderthread.cpp in the Mandlebrot example and it does not have this problem. Any help is much appreciated. I'm new to threading but I am pretty comfortable with Qt.

    Thanks!
    Seth

    InfiniteThread.h:
    Qt Code:
    1. #ifndef INFINITETHREAD_H
    2. #define INFINITETHREAD_H
    3.  
    4. #include <QThread>
    5. #include <QMutex>
    6. #include <QWaitCondition>
    7.  
    8. class InfiniteThread : public QThread
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. explicit InfiniteThread(QObject *parent = 0);
    14. ~InfiniteThread();
    15.  
    16. signals:
    17. void reportProgress(int pProg, QString message);
    18. void taskDone(bool pSuccess);
    19.  
    20. protected:
    21. void startTask();
    22. void run();
    23.  
    24. // Override this in the child class
    25. virtual bool centralTask() = 0;
    26.  
    27. QMutex mutex;
    28. QWaitCondition condition;
    29. bool restart;
    30. bool abort;
    31. };
    32.  
    33. #endif // INFINITETHREAD_H
    To copy to clipboard, switch view to plain text mode 

    InfiniteThread.cpp:
    Qt Code:
    1. #include "infinitethread.h"
    2.  
    3. InfiniteThread::InfiniteThread(QObject *parent) :
    4. QThread(parent)
    5. {
    6. restart = false;
    7. abort = false;
    8. }
    9.  
    10. InfiniteThread::~InfiniteThread()
    11. {
    12. mutex.lock();
    13. abort = true;
    14. condition.wakeOne();
    15. mutex.unlock();
    16.  
    17. wait();
    18. }
    19.  
    20. void InfiniteThread::startTask()
    21. {
    22. QMutexLocker locker(&mutex);
    23.  
    24. if (!isRunning()) {
    25. start(LowPriority);
    26. } else {
    27. restart = true;
    28. condition.wakeOne();
    29. }
    30. }
    31.  
    32. void InfiniteThread::run()
    33. {
    34. forever {
    35. // Perform central task
    36. bool result = centralTask();
    37. if(!restart) emit taskDone(result);
    38.  
    39. // Abort thread if requested
    40. if(abort) return;
    41.  
    42. // Restart when new central task is given
    43. mutex.lock();
    44. if(!restart) condition.wait(&mutex);
    45. restart = false;
    46. mutex.unlock();
    47. }
    48. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QThread::wait() causing crash

    The error says it all:

    pure virtual method called
    Your problem is this line:
    Qt Code:
    1. virtual bool centralTask() = 0;
    To copy to clipboard, switch view to plain text mode 

    I guess you implemented this in a subclass?
    Looking at the trace, I guess the subclass gets deleted first (obviously)?

    Edit: possible solution, move the run() code from your base class to the subclass.

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

    Olliebrown (24th September 2010)

  4. #3
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QThread::wait() causing crash

    Ahhh! Yup, that makes sense. I was thoroughly confused by the error as the only pure virtual function was CentralTask() and I was re-implementing it properly. It never occurred to me that if the sub-class get's deleted then this will no longer be the case. Good eye!

    Thanks!
    Seth

  5. #4
    Join Date
    Sep 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Smile Re: QThread::wait() causing crash

    That did the trick! I worked around the deletion order by moving the code that's in the destructor into a function called stopTask(). Then, I call this from the child's destructor so the run loop will get stopped BEFORE CentralTask() becomes a pure virtual function again. Worked like a charm.

    Here's the updated code:

    Seth

    Qt Code:
    1. #ifndef INFINITETHREAD_H
    2. #define INFINITETHREAD_H
    3.  
    4. #include <QThread>
    5. #include <QMutex>
    6. #include <QWaitCondition>
    7.  
    8. class InfiniteThread : public QThread
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. explicit InfiniteThread(QObject *parent = 0);
    14. ~InfiniteThread();
    15.  
    16. void startTask();
    17. void stopTask();
    18.  
    19. signals:
    20. void reportProgress(int pProg, QString message);
    21. void taskDone(bool pSuccess);
    22.  
    23. protected:
    24. void run();
    25.  
    26. // Override this in the child class
    27. virtual bool centralTask() = 0;
    28.  
    29. QMutex mutex;
    30. QWaitCondition condition;
    31. bool restart;
    32. bool abort;
    33. };
    34.  
    35. #endif // INFINITETHREAD_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "infinitethread.h"
    2.  
    3. InfiniteThread::InfiniteThread(QObject *parent) :
    4. QThread(parent)
    5. {
    6. restart = false;
    7. abort = false;
    8. }
    9.  
    10. InfiniteThread::~InfiniteThread()
    11. {
    12. }
    13.  
    14. void InfiniteThread::stopTask()
    15. {
    16. mutex.lock();
    17. abort = true;
    18. condition.wakeOne();
    19. mutex.unlock();
    20.  
    21. wait();
    22. }
    23.  
    24. void InfiniteThread::startTask()
    25. {
    26. QMutexLocker locker(&mutex);
    27.  
    28. if (!isRunning()) {
    29. start(LowPriority);
    30. } else {
    31. restart = true;
    32. condition.wakeOne();
    33. }
    34. }
    35.  
    36. void InfiniteThread::run()
    37. {
    38. forever {
    39. // Perform central task
    40. bool result = centralTask();
    41. if(!restart) emit taskDone(result);
    42.  
    43. // Abort thread if requested
    44. if(abort) return;
    45.  
    46. // Restart when new central task is given
    47. mutex.lock();
    48. if(!restart) condition.wait(&mutex);
    49. restart = false;
    50. mutex.unlock();
    51. }
    52. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QThread issues. Crash after 2058 runs.
    By zverj in forum Qt Programming
    Replies: 4
    Last Post: 15th October 2009, 10:13
  2. Weird Windows Vista Permissions Causing Qmake Crash
    By CrazyIvanovich in forum Qt Programming
    Replies: 1
    Last Post: 4th June 2009, 21:52
  3. QThread blocking wait()
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2009, 09:21
  4. QTreeWidget->clear() causing crash after sort
    By Chuk in forum Qt Programming
    Replies: 7
    Last Post: 3rd September 2007, 09:59
  5. Enter key causing program to exit
    By welby in forum Qt Programming
    Replies: 2
    Last Post: 9th March 2006, 16: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.