sleep or delay in qthread but crash
-
hey guys.
i write my multithread app.auto my = new My(); auto thread = new QThread(); my->moveToThread(thread);in the slot function(child thread) ,i need to pool some IO status, so i wrote many version for delay,but it crash in any way:
void delay(int ms){ if(QThread::currentThread() == m_myThread){ if(m_delayTimer2 == nullptr){ m_delayTimer2 = new QTimer(); m_delayTimer2->setInterval(1); m_delayTimer2->setTimerType(Qt::PreciseTimer); m_delayTimer2->setSingleShot(true); } if(m_delayEl2 == nullptr){ m_delayEl2 = new QEventLoop(); connect(m_delayTimer2,&QTimer::timeout,m_delayEl2,&QEventLoop::quit); } m_delayTimer2->start(msec); m_delayEl2->exec(QEventLoop::ExcludeUserInputEvents); // crash here } } //call it like this: while(1){ if(io() ==0) break; delay(1); }or like this:
if(QThread::currentThread() == m_myThread){ QEventLoop el; QTimer::singleShot(msec,&el,&QEventLoop::quit); el.exec(QEventLoop::ExcludeUserInputEvents); // crash here }cannot like this ,the signal will be blocked:
//QThread::msleep(msec); or: /*auto ret = ::WaitForSingleObject(m_evtHandle,msec); ::ResetEvent(m_evtHandle); if(ret == WAIT_OBJECT_0){ }else if(ret == WAIT_TIMEOUT){ }*/what will lead to crash? thanks in advance.
-
hey guys.
i write my multithread app.auto my = new My(); auto thread = new QThread(); my->moveToThread(thread);in the slot function(child thread) ,i need to pool some IO status, so i wrote many version for delay,but it crash in any way:
void delay(int ms){ if(QThread::currentThread() == m_myThread){ if(m_delayTimer2 == nullptr){ m_delayTimer2 = new QTimer(); m_delayTimer2->setInterval(1); m_delayTimer2->setTimerType(Qt::PreciseTimer); m_delayTimer2->setSingleShot(true); } if(m_delayEl2 == nullptr){ m_delayEl2 = new QEventLoop(); connect(m_delayTimer2,&QTimer::timeout,m_delayEl2,&QEventLoop::quit); } m_delayTimer2->start(msec); m_delayEl2->exec(QEventLoop::ExcludeUserInputEvents); // crash here } } //call it like this: while(1){ if(io() ==0) break; delay(1); }or like this:
if(QThread::currentThread() == m_myThread){ QEventLoop el; QTimer::singleShot(msec,&el,&QEventLoop::quit); el.exec(QEventLoop::ExcludeUserInputEvents); // crash here }cannot like this ,the signal will be blocked:
//QThread::msleep(msec); or: /*auto ret = ::WaitForSingleObject(m_evtHandle,msec); ::ResetEvent(m_evtHandle); if(ret == WAIT_OBJECT_0){ }else if(ret == WAIT_TIMEOUT){ }*/what will lead to crash? thanks in advance.
@QtTester
If you want to know why something "crashes" let it crash under debugger and look at stack trace.
I don't know what is wrong with your code, where you call what from etc.
But why do your work with a blocking delay anyway? Just set off a timer (repeating or single shot) in the thread, no blocking, no sleeping, noQEventLoopof your own, and do the work (like reading from I/O) on timeout in connected slot. -
Hi,
Beside the good points made by @JonB and based on your code, I would say uninitialized pointers.
@SGaist the pointer has been inited. and it is not crash at the begining. it crash at an unexpected time.
// this version will still crash, even no pointer. QEventLoop el; QTimer::singleShot(msec,&el,&QEventLoop::quit); el.exec(QEventLoop::ExcludeUserInputEvents);@JonB i am using debugger,but cannot analyse how it is going on.
unless i need to debug the qt source file? -
@SGaist the pointer has been inited. and it is not crash at the begining. it crash at an unexpected time.
// this version will still crash, even no pointer. QEventLoop el; QTimer::singleShot(msec,&el,&QEventLoop::quit); el.exec(QEventLoop::ExcludeUserInputEvents);@JonB i am using debugger,but cannot analyse how it is going on.
unless i need to debug the qt source file?@QtTester said in sleep or delay in qthread but crash:
// this version will still crash, even no pointer. QEventLoop el; QTimer::singleShot(msec,&el,&QEventLoop::quit); el.exec(QEventLoop::ExcludeUserInputEvents);What's the point of all this?
You probably don't need it when using a worker thread.Simplify your app / worker / thread class until it doesn't crash anymore
-
@QtTester said in sleep or delay in qthread but crash:
// this version will still crash, even no pointer. QEventLoop el; QTimer::singleShot(msec,&el,&QEventLoop::quit); el.exec(QEventLoop::ExcludeUserInputEvents);What's the point of all this?
You probably don't need it when using a worker thread.Simplify your app / worker / thread class until it doesn't crash anymore
-
@Pl45m4 need to query some io status:
//call it like this: dosomething(); while(1){ if(io() ==0) break; delay(1); } dootherthing();@QtTester said in sleep or delay in qthread but crash:
need to query some io status:
Then use a QTimer with the Worker-Object approach shown in the documentation: https://doc.qt.io/qt-6/qthread.html#details
-
@QtTester said in sleep or delay in qthread but crash:
need to query some io status:
Then use a QTimer with the Worker-Object approach shown in the documentation: https://doc.qt.io/qt-6/qthread.html#details
@Christian-Ehrlicher we may not write code like that. it is a long long complicate procedure, and will delay for many condition with many times.
-
@Christian-Ehrlicher we may not write code like that. it is a long long complicate procedure, and will delay for many condition with many times.
@QtTester
I already wrote above https://forum.qt.io/post/816186 that you should abandon all this event loop and waiting stuff. It's not complicated, it doesn't introduce extra delays and can be used many times.Otherwise do your best to debug your code. You have already said you have made a change per @SGaist and that has changed where the crash is, so who knows what is going on.
-
@Christian-Ehrlicher we may not write code like that. it is a long long complicate procedure, and will delay for many condition with many times.
@QtTester so either try to fix this crappy code or rewrite it - I would say the latter costs less, especially in the long run.
-
i trace the qt source file,it stop right here, is qstring .append() possible has a bug???,version is 5.14.1:
more:
@QtTester Before assuming a bug in Qt you should make sure it is not your code doing something wrong. The fact that the crash is in Qt code does not mean the issue is in Qt. Since you're writing quite convoluted code in this case I'm quite sure the issue is on your side.
-
This looks like two other threads in the last few days - don't mix debug and release libraries.
-
Usually, it is your own code and not Qt's (I've been using Qt for 20 years and this is still true for me). Go down the stacktrace until you find your own code. See how you call Qt and most likely there is some wrong value there. Also, since you are using more than one thread, look in the stacktraces of all threads that have your own code running.
-
This looks like two other threads in the last few days - don't mix debug and release libraries.
@Christian-Ehrlicher For the record, we seem to have a fair number of debug/release library mixing recently.
-
Q QtTester has marked this topic as solved on
-
@QtTester said in sleep or delay in qthread but crash:
I set qdebug level to qwarning, the crash disappeared! strange
This is not strange at all. You have just disabled any line with
qDebug()in your source. As I have said before your error is most likely in your own code. Now, that the erroneous memory address is not accessed byqDebug()anymore the bug vanishes. You can also just remove the offendingqDebug()line in your source (right where it crashed in your stacktrace) and everything works fine as well. Then, you can continue to useqDebug()everywhere else.