PDA

View Full Version : QThread crashes on exit



mhoover
25th August 2009, 03:40
QThread attempts to lock something when it exits using a QMutexLocker class.


void QThread::exit(int returnCode)
{
Q_D(QThread);
QMutexLocker locker(&d->mutex); // <------ Crashes here! d == 0x0
d->data->quitNow = true;
for (int i = 0; i < d->data->eventLoops.size(); ++i) {
QEventLoop *eventLoop = d->data->eventLoops.at(i);
eventLoop->exit(returnCode);
}
}

Just trying to declare the QMutexLocker causes a crash.

This looks like a Qt bug, but I'm not sure.

Any ideas? It looks like "d" might not have been allocated.

nish
25th August 2009, 06:16
now see... thousands of ppl use QThread everyday.. if it was so easy to crash it than it would have been found and fixed there and then... so the problem is in your case..
whenever your proggi crashes in Qt code, then make a minimal compilable example that would reproduce the bug and submit it to trolltech... it would also be helpful to answer you if you submit it here ...

lyuts
25th August 2009, 10:43
Is there core dump?

wysota
25th August 2009, 12:12
Aren't you calling exit() on an uninitialized QThread pointer by any chance?

mhoover
25th August 2009, 20:00
There is no core dump. How do you enable that again? I'm using RedHat if that matters.

In this case the threads are declared as member variables on the stack, so the threads can't be uninitialized pointers. I suppose there could be other uninitialized pointers ...

I realize there are a lot of people who use QThreads all the time, but I don't think a large portion of the herd is running in my direction (lots of threads containing networking classes). The other thing is I wonder how a private Qt-class variable is not initialized ...

wysota
25th August 2009, 21:47
In this case the threads are declared as member variables on the stack, so the threads can't be uninitialized pointers.
Sure they can.


The other thing is I wonder how a private Qt-class variable is not initialized ...

Probably because it was deleted or has not been initialized yet. Or... you have stack corruption.

mhoover
25th August 2009, 22:16
Thanks again, everyone for your help.

This solved the problem:


UDPThread::~UDPThread()
{
exit();
while( this->isRunning() == true ) {

}

}

My assumption was exit() didn't return until the thread was finished running, but it keeps going.

It looks like exit() stops the event loop and then stops the run() a bit later.

spirit
26th August 2009, 06:52
try to use "native" methods of QThread


UDPThread::~UDPThread()
{
if (isRunning()) {
quit();
wait();
}
}

mhoover
26th August 2009, 20:19
Oh, right.

I was trying to use the intellisense thing to quickly see what methods were available.

I will change it.