Results 1 to 4 of 4

Thread: QThread::wait() causing crash

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

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