qtpromise icon indicating copy to clipboard operation
qtpromise copied to clipboard

The QObject::deleteLater() in .then doesn't work.

Open qq824810885 opened this issue 3 years ago • 3 comments

Here is my test code:


QObject *testObj = new QObject();
QObject::connect(testObj,&QObject::destroyed,[]{
    qInfo() << "obj deleted.";
});
auto promsie = ...
promise.then([=]{
    testObj->deleteLater();
});

qq824810885 avatar Dec 17 '21 03:12 qq824810885

There is nothing special about deleteLater() that would prevent it from working in then(). This works as expected:

void testDelete() {
    auto obj = QPointer{new QObject{}};
    auto p1 = QtPromise::resolve();  // your '...'
    auto p2 = p1.then([obj]{ obj->deleteLater(); });
    p1.wait();
    QVERIFY(!obj.isNull());
    p2.wait();
    QVERIFY(obj.isNull());
}

Perhaps there is something in your "..." that doesn't resolve? Or perhaps in your test you awaited p1 but didn't wait for p2?

pwuertz avatar Dec 20 '21 12:12 pwuertz

Yes,I didn't wait p2, but why do that?

PS : I test the code above in main thread.

qq824810885 avatar Dec 23 '21 04:12 qq824810885

Yes,I didn't wait p2, but why do that?

The execution of the promises is asynchronous. So if you don't wait (or enter an event loop), then the promise actions are not executed.

And .then() returns a new promise. If you don't wait for this new promise, then the .then() block is not executed.

j-ulrich avatar Jan 02 '22 23:01 j-ulrich