简体   繁体   中英

qt QCoreApplication::exec() crash on ubuntu 12.04

I am developing a system application with signal slot on sockets in 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
}

I have a processTexts function that is either a slot for a thread and also emits a signal to another thread. If I omit the second emit in Handler the problem is not solved but program crashes somewhat later. But it does not disapear. it creashes after a minute or two. program processes texts that comes from UDP socket. and sends it via another QTcpSocket to another program.

The crash signature is :

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]

I analyzed it with valgrind and it says:

==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)

I analyzed the program and I removed all my bugs I do not know if it is a Qt bug or ubuntu bug or GCC or my fault.

Besides I wanted to know if anybody tried a system application with more than 20 events per second. It crashes after a while.

Thank you all in advance.

SocketNotifier implementation is not thread safe. That means that you are not allowed to control it from different threads.

If you have Qt compiled without QT_NO_DEBUG you will receive warning that you control it from different threads:

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
//....
}

Please check if all member of object, that is moved to thread, are created as a child of this object. Or just switch off QT_NO_DEBUG.

BTW: I struggled with similar problem and the crash appears after few hours of usage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM