[SOLVED]Object delete incorrect in the QThread



  • Hi,
    I'm learning QThread from Qt Project website,and a few questions make me confused a lot,my code is very simple :
    @
    void open(QString servername){
    PipePiper *worker = new PipePiper(servername);
    worker->moveToThread(&workerThread);
    connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
    connect(this, SIGNAL(close()), worker, SLOT(deleteLater()));
    workerThread.start();
    }

    signals:
    void close(void);
    @
    I called open several times and I thought it will delete single one PipePiper object when signal close() was triggered,but it turns out it will delete all my workers which is absolutely not my purpose.
    Thanks for any advice.



  • oh,I see what problem is,signal close will trigger every worker->deleteLater.
    My way is totally wrong,but I still don't know how to fix it.



  • Please look at the first example in the QThread docu (you can click on the green QThread class in your code above to get there): They have a Worker object (your PipePiper) and a Controller object (your outside class).



  • [quote author="msue" date="1408084996"]Please look at the first example in the QThread docu (you can click on the green QThread class in your code above to get there): They have a Worker object (your PipePiper) and a Controller object (your outside class).[/quote]

    thanks,I fixed it,it may have a little memory leak but i will continue work on it.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.