I have to downgrade a project from QT5 to QT4 and get a weird buffer overrun error when doing so. Here's my code:
I create a QThread like so:
thread = new QThread;
reader = new Reader();
reader->setParams(samplingRate);
reader->moveToThread(thread);
connect(thread, SIGNAL(started()), reader, SLOT(read()));
connect(reader, SIGNAL(finished()), thread, SLOT(quit()));
connect(thread, SIGNAL(finished()), this, SLOT(threadFinished()));
connect(reader, SIGNAL(finished()), reader, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
connect(reader, SIGNAL(errorMsg(QString)), this, SLOT(threadErrorMsg(QString)));
thread->start();
In my thread I have the following code:
try {
while(condition) {
...something
}
} catch(Exception e) {
emit errorMsg("Error");
}
emit finished();
And the Slots in my main thread look like this:
void MainWindow::threadFinished() {
reader = NULL;
delete thread;
thread = NULL;
}
void MainWindow::threadErrorMsg(QString message) {
QMessageBox::critical(this, "Error", ("Error: " + message).toStdString().c_str(), QMessageBox::Ok);
}
All of this worked well in QT5. The error message box was displayed correctly and the thread was destroyed. In QT4 (4.8.1) however I get a buffer overrun error when an error occurs and errorMsg() is emitted. The buffer overrun does not occur if I don't emit the errorMsg("Error") and destroy the thread only by calling finished(). Any ideas on what's wrong here?
Update: If i don't access the QString in threadErrorMsgit does work. Like so:
void MainWindow::threadErrorMsg(QString message) {
QMessageBox::critical(this, "Error", "Error", QMessageBox::Ok);
}
What am I doing wrong?
void MainWindow::threadFinished() {
reader = NULL;
delete thread;
thread = NULL;
}
Deleting the thread directly in the slot is not advised. You should use the deleteLater function.
thread->deleteLater();
However, you've already setup the thread to be deleted in the connection
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
So you're now trying to delete it twice!
As for the buffer-overrun, I suspect something has corrupted memory before the error has occurred.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.