我正在使用QSerialPort与硬件设备进行通信。新数据不会发出“ readyRead”信号,因此我决定使用QThread编写读取线程。

这是代码:

void ReadThread::run()
{
    while(true){
        readData();
        if (buffer.size() > 0) parseData();
    }
}




void ReadThread::readData()
{
    buffer.append(device->readAll();
}


缓冲区是一个专用的QByteArray,设备是指向QSerialPort的指针。 ParseData将解析数据并发出一些信号。保留parseData时清除缓冲区。

这可以正常工作,但是经过一段时间(有时10秒,有时是1小时),程序将通过SIGSEGV崩溃并显示以下跟踪信息:

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff3498700 (LWP 24870)]
malloc_consolidate (av=av@entry=0x7fffec000020) at malloc.c:4151
(gdb) bt
#0  malloc_consolidate (av=av@entry=0x7fffec000020) at malloc.c:4151
#1  0x00007ffff62c2ee8 in _int_malloc (av=av@entry=0x7fffec000020, bytes=bytes@entry=32769) at malloc.c:3423
#2  0x00007ffff62c4661 in _int_realloc (av=av@entry=0x7fffec000020, oldp=oldp@entry=0x7fffec0013b0, oldsize=oldsize@entry=64, nb=nb@entry=32784) at malloc.c:4286
#3  0x00007ffff62c57b9 in __GI___libc_realloc (oldmem=0x7fffec0013c0, bytes=32768) at malloc.c:3029
#4  0x00007ffff70d1cdd in QByteArray::reallocData(unsigned int, QFlags<QArrayData::AllocationOption>) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
#5  0x00007ffff70d1f07 in QByteArray::resize(int) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
#6  0x00007ffff799f9fc in free (bytes=<optimized out>, this=0x609458)
    at ../../include/QtSerialPort/5.3.2/QtSerialPort/private/../../../../../src/serialport/qt4support/include/private/qringbuffer_p.h:140
#7  read (maxLength=<optimized out>, data=<optimized out>, this=0x609458)
    at ../../include/QtSerialPort/5.3.2/QtSerialPort/private/../../../../../src/serialport/qt4support/include/private/qringbuffer_p.h:326
#8  QSerialPort::readData (this=<optimized out>, data=<optimized out>, maxSize=<optimized out>) at qserialport.cpp:1341
#9  0x00007ffff722bdf0 in QIODevice::read(char*, long long) () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
#10 0x00007ffff722cbaf in QIODevice::readAll() () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
#11 0x00007ffff7bd0741 in readThread::readData (this=0x6066c0) at ../reader.cpp:212
#12 0x00007ffff7bc80d0 in readThread::run (this=0x6066c0) at ../reader.cpp:16
#13 0x00007ffff70cdd2e in ?? () from /usr/lib/x86_64-linux-gnu/libQt5Core.so.5
#14 0x00007ffff6e1c0a4 in start_thread (arg=0x7ffff3498700) at pthread_create.c:309
#15 0x00007ffff632f04d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111


由于它随机出现,因此我不确定如何正确重现该问题。如果我在while循环中注释掉“ readData()”,则崩溃不再出现(当然,不能解析任何数据)。

有人知道这可能是什么吗?

最佳答案

什么是缓冲区?可能是另一个线程正在从缓冲区读取数据并随后将其清除吗?

尝试锁定它(以及线程之间共享的所有其他数据),例如带互斥锁

QMutex mx; // could be also member of the ReadThread class

void ReadThread::readData()
{
    mx.lock();
    buffer.append(device->readAll();
    mx.unlock();
}


并在从另一个线程读取并清除缓冲区的代码中执行相同的操作(我不做假设,这是parseData())

另一种可能是parseData()调用了一些在GUI-Thread中运行的代码。这在Qt4和Qt5中均不起作用

关于c++ - 如果在while循环中调用QSerialPort::readAll()会导致SIGSEGV/SIGABRT,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31723468/

10-13 08:28