2

删除 serialPort 会在调试版本中导致此消息:

QCoreApplication::sendEvent 中的 ASSERT 失败:“无法向其他线程拥有的对象发送事件。当前线程 c0a528。接收器 ''(类型为 'QSerialPort')是在线程 c76850 中创建的”,文件 kernel\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;  
}

编辑1:佩佩的建议:

如果我将 QSerialReader 更改为 QDataReader 中的成员,问题就消失了。但是当使用 QSerialPort::open 打开端口时,会出现此错误:

QObject:无法为不同线程中的父级创建子级。(父级为 QSerialPort(0xdf6930),父级线程为 QThread(0xd8a528),当前线程为 QThread(0xdf6850) QObject:无法为位于不同线程中的父级创建子级。(父级为 QSerialPort(0xdf6930),父级线程为 QThread (0xd8a528),当前线程是QThread(0xdf6850)

QSerialPort::open(xx) 在 serialReader.readData() 内部调用;

所以仍然......一些令人讨厌的问题。几年前我在 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  
4

1 回答 1

2
serialReader=new QSerialReader(this);

不要这样做。父母和孩子必须生活在同一个线程中。在您的情况下,父母(QThread)生活在自己的线程中,而孩子(QSerialReader)生活在您产生的线程中。你不能把它分配在堆栈上吗?

于 2013-05-30T12:00:55.467 回答