Is it safe to call deleteLater right after emitting a signal with the object?

扶醉桌前 提交于 2020-12-12 05:41:05

问题


I believe the following code is safe, but I have not been able to confirm it:

void someMethod(Process *process) {
    emit signalWithProcess(process);
    process->deleteLater();
}

I'm not sure if process still exists when the signals are called and auto or queued connections are used.

For direct connections, the code above is correct because the slots are called synchronously. AFAIK, for slots on the same thread as the object, it is also correct because deleteLater() posts a new event to the object's event loop. However, the signal events are posted earlier so they run earlier.

However, for queued connections on different threads I do not understand how it works and whether the code above is safe. My gut-feeling says yes, but I have not been able to verify it.


For example, the process object above belongs to thread A and there's a queued connection to another object on thread B. When someMethod() runs, it adds the queued signals to thread B's event loop and adds the deleteLater event to thread A's event loop.

Unless there is some way to block thread A from deleting the object while the slot is run in thread B, this seems not to be safe. Unfortunately, I have not found anything related to that in the source code or the documentation.


Similar question (but without deleteLater): Is it safe to emit signal passing QObject pointer as parameter right before the passed object is going to be destroyed?

来源:https://stackoverflow.com/questions/65184209/is-it-safe-to-call-deletelater-right-after-emitting-a-signal-with-the-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!