删除serialPort会在调试版本中导致此消息:
在QCoreApplication :: sendEvent中失败:“无法向不同线程拥有的对象发送事件。当前线程c0a528。接收器''(类型'QSerialPort')是在线程c76850中创建的”,文件内核\ qcoreapplication.cpp ,第532行
当主窗口关闭时,~QSerialReader中会触发实际错误。这是代码。
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
runnerThread=new QThread(this);
/* - connect thread start signal to qrunnerthread's runLoop()
this starts the main job loop
- connect thread finished signal to mainwindows' deleteLater
to ensure proper deletion */
qDataReader = new QDataReader();
connect(runnerThread, SIGNAL(started()), qRunnerThread, SLOT(runLoop()));
connect(runnerThread, SIGNAL(finished()), this, SLOT(deleteLater()));
qDataReader->moveToThread(runnerThread);
runnerThread.start();
}
MainWindow::~MainWindow()
{
runnerThread->exit();
}
//slot that runs when thread is started
void QDataReader::runLoop() {
/* this code runs in different thread than QDataReader */
//serialPort parent will be QThread (runnerThread)
serialReader=new QSerialReader(this);
while(doStuff) {
QString data=serialReader.readData();
emit dataReceived(data);
}
}
QDataReader::~QDataReader() {
delete runner;
}
QSerialReader::QSerialReader(QObject* parent) {
//serialPort parent will be QSerialReader
serialPort = new QSerialPort(this);
}
QSerialReader::~QSerialReader() {
/*
deleting serialPort causes this message in debug builds:
ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread c0a528. Receiver '' (of type 'QSerialPort') was created in thread c76850", file kernel\qcoreapplication.cpp, line 532
*/
delete serialPort;
}
edit1:peppe的建议:
如果我将QSerialReader更改为QDataReader中的成员,则问题就消失了。但是当用QSerialPort :: open打开端口时,会出现此错误:
QObject:无法为不同线程中的父级创建子级。 (Parent是QSerialPort(0xdf6930),父线程是QThread(0xd8a528),当前线程是QThread(0xdf6850) QObject:无法为位于不同线程中的父级创建子级。 (Parent是QSerialPort(0xdf6930),父线程是QThread(0xd8a528),当前线程是QThread(0xdf6850)
在serialReader.readData()中调用QSerialPort :: open(xx);
所以仍然......一些令人讨厌的问题。几年前,当我用Java编码时,我没有这样的问题。
stack trace when CSerialReader::openPort is called. this calls QSerialPort::open. Clearly this is run in other thread than main.
0 CSerialReader::openPort serialreader.cpp 480 0xff7211
1 CSerialReader::readData serialreader.cpp 209 0xff4e1d
2 QDataReader::runLoop qdatareader.cpp 138 0xffa3fe
3 QDataReader::qt_static_metacall moc_qdatareader.cpp 62 0x1040824
4 QMetaObject::activate qobject.cpp 3539 0x669b0e14
5 QThread::started moc_qthread.cpp 113 0x66a29979
6 QThreadPrivate::start qthread_win.cpp 345 0x66816918
7 _callthreadstartex threadex.c 314 0xf71a293
8 _threadstartex threadex.c 297 0xf71a224
9 BaseThreadInitThunk kernel32 0x74cd3677
10 __RtlUserThreadStart ntdll32 0x76f9c002
11 _RtlUserThreadStart ntdll32 0x76f9bfd5
创建CSerialReader时的堆栈跟踪(作为成员变量)。这在主线程中调用。
0 CSerialReader::CSerialReader Runner.cpp 48 0x9d3d14
1 QDataReader::QDataReader qdatareader.cpp 10 0x9d8fa4
2 MainWindow::MainWindow mainwindow.cpp 72 0x9c61eb
3 main main.cpp 105 0x9c4adc
4 WinMain qtmain_win.cpp 131 0xa273ba
5 __tmainCRTStartup crtexe.c 547 0xa269e0
6 WinMainCRTStartup crtexe.c 371 0xa2676f
7 BaseThreadInitThunk kernel32 0x74cd3677
8 __RtlUserThreadStart ntdll32 0x76f9c002
9 _RtlUserThreadStart ntdll32 0x76f9bfd5
答案 0 :(得分:2)
serialReader=new QSerialReader(this);
不要这样做。父母和孩子必须生活在同一个线索中。在您的情况下,父(QThread)生活在自己的线程中,子(QSerialReader)生成在您生成的线程中。你不能只把它分配到堆栈上吗?