qtpromise
qtpromise copied to clipboard
The QObject::deleteLater() in .then doesn't work.
Here is my test code:
QObject *testObj = new QObject();
QObject::connect(testObj,&QObject::destroyed,[]{
qInfo() << "obj deleted.";
});
auto promsie = ...
promise.then([=]{
testObj->deleteLater();
});
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
?
Yes,I didn't wait p2, but why do that?
PS : I test the code above in main thread.
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.