我正在开发一个在 qt、ubuntu 中的套接字上带有信号槽的系统应用程序
void Receiver::startServer()
{
....
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(receiveFunc()));
}
void Receiver::receiveFunc()
{
emit receiveForwarder();
{
Handler::start()
{
receiver= new Receiver();
thread=new QThread()
receiver->moveToThread(thread);
receiver->startServer();
connect(receiver, SIGNAL(receiveForwarder()), this, SLOT(processTexts()));
}
Handler::processTexts()
{
//emit another signal that is in another thread
}
我有一个 processTexts 函数,它既是线程的插槽,也向另一个线程发出信号。如果我在 Handler 中省略了第二次发出,则问题并未解决,但程序稍后会崩溃。但它并没有消失。一两分钟后它会折断。程序处理来自 UDP 套接字的文本。并通过另一个 QTcpSocket 将其发送到另一个程序。
崩溃签名是:
0[bt:0]/application/collector/main.cpp::exceptionHandler(int)::30
./collector(_Z16exceptionHandleri+0x56)[0x422156]
/lib/x86_64-linux-gnu/libc.so.6(+0x364c0)[0x7fbf348f54c0]
/usr/lib/x86_64-linux-gnu/libQtCore.so.4(+0x1a9b24)[0x7fbf35558b24]
/lib/x86_64-linux-gnu/libglib-2.0.so.0(g_main_context_check+0x183)[0x7fbf33db1a33]
/lib/x86_64-linux-gnu/libglib-2.0.so.0(+0x47f96)[0x7fbf33db1f96]
/lib/x86_64-linux-gnu/libglib-2.0.so.0(g_main_context_iteration+0x34)[0x7fbf33db2124]
/usr/lib/x86_64-linugnu-libQtCore.so.4(_ZN20QEventDispatcherGlib13processEventsE6QFlagsIN10QEventLoop17ProcessEven tsFlagEE+0xd6)[0x7fbf35559426]
/usr/lib/x86_64-linux-gnu/libQtCore.so.4(_ZN10QEventLoop13processEventsE6QFlagsINS_17ProcessEventsFlagEE+0x32) [0x7fbf35528c82]
/usr/lib/x86_64-linux-gnu/libQtCore.so.4(_ZN10QEventLoop4execE6QFlagsINS_17ProcessEventsFlagEE+0xf7) [0x7fbf35528ed7]
/usr/lib/x86_64-linux-gnu/libQtCore.so.4(_ZN16QCoreApplication4execEv+0x87)[0x7fbf3552df67]
./collector(main+0x216)[0x4234c6]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7fbf348e076d]
我用 valgrind 分析了它,它说:
==31981== Conditional jump or move depends on uninitialised value(s)
==31981== at 0x556AB14: ??? (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x6C2AA32: g_main_context_check (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== by 0x6C2AF95: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== by 0x6C2B123: g_main_context_iteration (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== by 0x556B3BE: QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x553AC81: QEventLoop::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x553AED6: QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x553FF66: QCoreApplication::exec() (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x4234C5: main (main.cpp:170)
==31981==
==31981== Invalid read of size 2
==31981== at 0x556AB45: ??? (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x6C2AA32: g_main_context_check (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== by 0x6C2AF95: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== by 0x6C2B123: g_main_context_iteration (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== by 0x556B3BE: QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x553AC81: QEventLoop::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x553AED6: QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x553FF66: QCoreApplication::exec() (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x4234C5: main (main.cpp:170)
==31981== Address 0xa8bf0c6 is 6 bytes inside a block of size 16 free'd
==31981== at 0x4C2A4BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31981== by 0x556AB44: ??? (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x6C2AA32: g_main_context_check (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== by 0x6C2AF95: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== by 0x6C2B123: g_main_context_iteration (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== by 0x556B3BE: QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x553AC81: QEventLoop::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x553AED6: QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x553FF66: QCoreApplication::exec() (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x4234C5: main (main.cpp:170)
==31981==
==31981== Invalid read of size 2
==31981== at 0x556AB4D: ??? (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x6C2AA32: g_main_context_check (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== by 0x6C2AF95: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== by 0x6C2B123: g_main_context_iteration (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== by 0x556B3BE: QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x553AC81: QEventLoop::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x553AED6: QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x553FF66: QCoreApplication::exec() (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x4234C5: main (main.cpp:170)
==31981== Address 0xa8bf0c4 is 4 bytes inside a block of size 16 free'd
==31981== at 0x4C2A4BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31981== by 0x556AB44: ??? (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x6C2AA32: g_main_context_check (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== by 0x6C2AF95: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== by 0x6C2B123: g_main_context_iteration (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== by 0x556B3BE: QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x553AC81: QEventLoop::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x553AED6: QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x553FF66: QCoreApplication::exec() (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x4234C5: main (main.cpp:170)
==31981==
==31981== Thread 2:
==31981== Invalid write of size 2
==31981== at 0x6C281FA: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== by 0x6C291D1: g_source_add_poll (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== by 0x556B7A9: QEventDispatcherGlib::registerSocketNotifier(QSocketNotifier*) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x5145E59: QUdpSocket::readDatagram(char*, long long, QHostAddress*, unsigned short*) (in /usr/lib/x86_64-linux-gnu/libQtNetwork.so.4.8.1)
==31981== by 0x4205F1: LogReceiver::receiveLog() (udpSocket.cpp:52)
==31981== by 0x42B630: LogReceiver::qt_static_metacall(QObject*, QMetaObject::Call, int, void**) (moc_udpSocket.cpp:54)
==31981== by 0x5555445: QObject::event(QEvent*) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x553BE9B: QCoreApplication::notifyInternal(QObject*, QEvent*) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x553FC69: QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x556AF92: ??? (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x6C2AC99: g_main_context_dispatch (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== by 0x6C2B05F: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== Address 0xa8bf0c6 is 6 bytes inside a block of size 16 free'd
==31981== at 0x4C2A4BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31981== by 0x556AB44: ??? (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x6C2AA32: g_main_context_check (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== by 0x6C2AF95: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== by 0x6C2B123: g_main_context_iteration (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.1)
==31981== by 0x556B3BE: QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x553AC81: QEventLoop::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x553AED6: QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x553FF66: QCoreApplication::exec() (in /usr/lib/x86_64-linux-gnu/libQtCore.so.4.8.1)
==31981== by 0x4234C5: main (main.cpp:170)
我分析了程序并删除了所有我不知道是 Qt 错误或 ubuntu 错误或 GCC 还是我的错的错误。
此外,我想知道是否有人尝试过每秒处理超过 20 个事件的系统应用程序。它在一段时间后崩溃。
谢谢大家。