我正在使用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 bug或ubuntu bug或GCC或我的错。
此外,我想知道是否有人尝试过每秒超过20个事件的系统应用程序。它会在一段时间后崩溃。
提前谢谢大家。
答案 0 :(得分:0)
SocketNotifier实现不是线程安全的。这意味着您不能从不同的线程控制它。
如果您在没有QT_NO_DEBUG的情况下编译Qt,您将收到警告,您可以从不同的线程控制它:
void QEventDispatcherGlib::unregisterSocketNotifier(QSocketNotifier *notifier)
{
Q_ASSERT(notifier);
#ifndef QT_NO_DEBUG
int sockfd = notifier->socket();
if (sockfd < 0) {
qWarning("QSocketNotifier: Internal error");
return;
} else if (notifier->thread() != thread()
|| thread() != QThread::currentThread()) {
qWarning("QSocketNotifier: socket notifiers cannot be disabled from another thread");
return;
}
#endif
//....
}
请检查移动到线程的对象的所有成员是否都是作为此对象的子对象创建的。或者只是关掉QT_NO_DEBUG。
顺便说一句:我遇到了类似的问题,经过几个小时的使用后就出现了崩溃。