Application crash on emit signal
-
I have a strange issue with one of my projects currently, where an application crashes on emitting a signal. I've never had anything like this happen before, so I have absolutely no idea where or how to start with debugging this.
- I'm currently using Qt 6.2.2 (from source, debug build)
- The relevant signal is being emitted from an object that is an instance of a plugin which indirectly inherits from QObject, QRunnable, and a plugin interface (in that order) that was built with the same environment/options/etc. I load it and then run the runnable in another thread via QThreadPool::start()
- The crash really does happen on emit. It doesn't matter whether it's connected to a slot or not. Particularly, the last few lines listed in the debugger when it crashes are:
function | file:line | linecontents
DummyWorkerSS::run | dummyworkerss.cpp:35 | Q_EMIT signal_taskFinished(_vmap);
DummyWorkerSS::signal_taskFinished | moc_dummyworkerss.cpp:142 | QMetaObject::activate(this, &staticMetaObject, 0, _a);
QMetaObject::activate | qobject.cpp:3968 | --- (where is that file?)
doActivate<false> | qobject.cpp:3788 | ---
What are the likely suspects / how should I go about tracking this problem down?
-
Please provide some code on how you call it, what you're doing.
-
@A-A-SEZEN said in Application crash on emit signal:
You may be trying to access a memory point that no longer exists.
I think this is probably closest to the truth of the matter. Debugging multi-threaded apps is a PITA, and I've found source level debuggers to be quite limited in that regard. Thread syncronization ie does the thread exist and in a state you expect when you you try to communicate with/thru it...is often hard to determine. You should resort to some good ole fashion cerr output debugging to track down where this crash really occurs. the cause probably preceded the emit where you think it is happening...and it the emit trying to deliver to a thread that no longer exist and the real crash is the nonexistent receiver?
-
It took me a while to get to the bottom of this because the error I was getting was neither straightforward (nor consistent after I switched to a debug build), but here is, in essence, what happened:
I was keeping track of references to the runners with QSharedPointers. I had popped the reference off the list, put it in a QVariant, and passed it along with the runner. Evidently, this didn't prevent the reference count from dropping to zero, and the object was deleted some time shortly after the thread started executing. It was just a dummy object for testing, so it managed to complete execution before the objects' deletion, but wasn't able to live long enough to emit the signal.