Search code examples
c++qtpure-virtual

QThreadPool calls pure virtual function of parent class QRunnable


During runtime I get the error message: "pure vitual function called".

QThreadpool seems to call the pure virtual void run() of the parent class QRunnable, instead off void run() in the derived class Bm. Strangely enough the if I ry to call the function manually with b_1.run();, there is no problem during runtime.

Here is my class implentation:

class Bm : public QRunnable
{ 
    public:
    void run()
    {
        test();
    }
    private:
         void test();
 };

Here is my main function where the error happens.

int main()
{  
    QThreadPool pool;
    pool.setMaxThreadCount(1);
    BM b_1;

    pool.start(&b_1);

    return 0;
}

My Question: Why doesnt Qthreadpool use Bm::run() over QRunnble::run()?


Solution

  • The thread objects gets deleted when going out of the main() function scope even before the QThreadPool calls IRunnable::run(). Using QThreadPool::waitForDone() before returning will assure the thread being executed.