缓冲区溢出使用Qt线程和信号

缓冲区溢出使用Qt线程和信号

本文介绍了缓冲区溢出使用Qt线程和信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将一个项目从QT5降级到QT4,并得到一个奇怪的缓冲区溢出错误这样做。这是我的代码:



我创建一个QThread如此:

  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();

在我的线程中,我有以下代码:

  try {
while(condition){
... something
}
} catch(Exception e){
emit errorMsg(Error);
}
emit finished();

我的主线程中的Slots如下:

  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);
}

所有这些都在QT5中运行良好。错误消息框正确显示,线程已销毁。然而在QT4(4.8.1)中,当发生错误和发出errorMsg()时,我得到缓冲区溢出错误。如果我不发出errorMsg(Error)并且只通过调用finished()来销毁线程,就不会发生缓冲区溢出。任何想法,在这里错了什么?



更新:
如果我不访问QString在threadErrorMsgit工作。像这样:

  void MainWindow :: threadErrorMsg(QString message){
QMessageBox :: critical ,Error,QMessageBox :: Ok);
}

我做错了什么?

解决方案
  void MainWindow :: threadFinished(){
reader = NULL;
delete thread;
thread = NULL;
}

不建议直接删除插槽中的线程。您应该使用deleteLater函数。

  thread-> deleteLater但是,您已经在连接中设置要删除的主题

$ b




$ b

  connect(thread,SIGNAL(finished()),thread,SLOT(deleteLater 

因此,您现在尝试删除它两次!



对于缓冲区溢出,我怀疑在错误发生之前,有东西损坏了内存。


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.

这篇关于缓冲区溢出使用Qt线程和信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:20