Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, December 2006
QT UDP / Threading Problem


Message 1 in thread

I am writing a program in QT which must listen for data on multiple UDP
sockets.  The program crashed unexpectedly whenever both sockets were
actively receiving data.  The large program uses two separate event-loops,
but a smaller, non-GUI test program I wrote has the same problem.  Here is
the source code:

#------------------------------------------------------------------
#include <QtCore>
#include <QApplication>
#include <QtNetwork>
#include <QThread>
#include <iostream>
using std::cout; using std::endl;

class Thread : public QThread
{
public:
int port;
int count;
Thread (int p)
    {
    port=p;
    }
protected:
void run ()
    {
    char data[64];
    int length;
    QUdpSocket socket;
    socket.bind(port);
    while (true) {
        socket.waitForReadyRead();
        length = socket.readDatagram(data,63);
        count++;
        if (count%1000==0) cout<<"Received 1000 packets on
"<<port<<"."<<endl;
        }
    }
};

int main(int argc, char **argv)
{
QApplication app(argc, argv);
Thread listen1(22222);
listen1.start();
Thread listen2(33333);
listen2.start();
listen1.wait();
listen2.wait();
}
#---------------------------------------------------------------

A separate program sends small bits of data on ports 22222 and 33333 at
about 2,000 packets/s (4 bytes of data -- the UDP packet size is clearly
larger).  The original program receives larger packets at a similar rate (26
bytes of data).

The program runs for a little while and then shuts down after usually
10s-1min.  Here is the debug output:

#---------------------------------------------------------------
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1210799184 (LWP 4260)]
0x48d42407 in g_slist_prepend () from /usr/lib/libglib-2.0.so.0
(gdb) info thread
  3 Thread -1219191888 (LWP 4261)  0x48d4279c in g_slist_remove () from
/usr/lib/libglib-2.0.so.0
* 2 Thread -1210799184 (LWP 4260)  0x48d42407 in g_slist_prepend () from
/usr/lib/libglib-2.0.so.0
  1 Thread -1208699200 (LWP 4257)  0xb7f6a410 in ?? ()
(gdb) backtrace
#0  0x48d42407 in g_slist_prepend () from /usr/lib/libglib-2.0.so.0
#1  0x48d2e9cc in g_source_add_poll () from /usr/lib/libglib-2.0.so.0
#2  0x48fe490d in QEventDispatcherGlib::registerSocketNotifier () from
/usr/lib/libQtCore.so.4
#3  0x48fd9e8c in QSocketNotifier::setEnabled () from
/usr/lib/libQtCore.so.4
#4  0x490a0953 in QNativeSocketEngine::setReadNotificationEnabled () from
/usr/lib/libQtNetwork.so.4
#5  0x490accb6 in QUdpSocket::readDatagram () from
/usr/lib/libQtNetwork.so.4
#6  0x0804917c in Thread::run (this=0xbf9dbba0) at qtbreakhost.cpp:26
#7  0x48f2fff0 in QThreadPrivate::start () from /usr/lib/libQtCore.so.4
#8  0x48bf4371 in start_thread () from /lib/tls/libpthread.so.0
#9  0x48a5bffe in clone () from /lib/tls/libc.so.6
#---------------------------------------------------------------

As you can see, it crashes on a glib linked-list operation.  Any ideas?

thanks,
joe

Message 2 in thread

On 12/12/06, joe lawrence <infinitejoe@xxxxxxxxx> wrote:
> I am writing a program in QT which must listen for data on multiple UDP
> sockets.  The program crashed unexpectedly whenever both sockets were
> actively receiving data.  The large program uses two separate event-loops,
> but a smaller, non-GUI test program I wrote has the same problem.  Here is
> the source code:

[snip]

> As you can see, it crashes on a glib linked-list operation.  Any ideas?

Why are you using GTK+ in a Qt application? Otherwise I'd say, report
the bug to Trolltech.

-- 
 [ signature omitted ] 

Message 3 in thread

Sorry, I initially sent this only to Robin.  I am new to this group, but
thanks for the quick responses.  Please read:

As you can see, the program does not explicitly include GTK libraries.  I am
puzzled.  My development system is CentOS 4.4 on x86, but the problem is
repeatable on an Ubuntu 6.10 machine (both running GNOME, admittedly).

If you want to try for yourself, here is the client program (just qmake
-project; add "QT += network" to .pro file; qmake; make):
#-----------------------------------------------------------------------------

#include <QtCore>
#include <QApplication>
#include <QtNetwork>
#include <QThread>
#include <iostream>
using std::cout; using std::endl;

class Thread : public QThread
{
public:
int port;
int count;
Thread (int p)
    {
    port=p;
    }
protected:
void run ()
    {
    char data[] = "data";
    int length;
    QUdpSocket socket;
    while (true) {
        socket.writeDatagram(data, 4, QHostAddress("127.0.0.1"), port);
        usleep(1000); //value may need to be modified (probably lowered) on
your system
        count++;
        if (count%1000==0) cout<<"Sent 1000 packets on "<<port<<"."<<endl;
        }
    }
};

int main(int argc, char **argv)
{
QApplication app(argc, argv);
Thread write1(33333);
write1.start();
Thread write2(22222);
write2.start();

write1.wait();
write2.wait();
}
#-----------------------------------------------------------------------------

Andreas:  the program has the same crash without the cout call (but for my
own info:  is cout not thread-safe?).  Debug output is from the example.
Backtrace from the larger program is similar -- crashing on a gslist
function, and the larger program does not call cout.

thanks

Message 4 in thread

On 12.12.06 16:52:44, joe lawrence wrote:
> As you can see, the program does not explicitly include GTK libraries.  I am
> puzzled.

For some reason Qt uses its glib-eventloop stuff, so your app really
doesn't use GLib (not GTK, just glib).

> Andreas:  the program has the same crash without the cout call (but for my
> own info:  is cout not thread-safe?).

I have no idea, but I wouldn't count on it "out of the blue", the C++
docs should know this for sure.

> Debug output is from the example.

Weird, I'd send the example to qt-bugs at trolltech com.

Andreas

-- 
 [ signature omitted ] 

Message 5 in thread

Andreas Pakulat wrote:
> > Andreas:  the program has the same crash without the cout call (but for my
> > own info:  is cout not thread-safe?).
> 
> I have no idea, but I wouldn't count on it "out of the blue", the C++
> docs should know this for sure.

Standard C++ does not know anything about threads.

Andre'

--
 [ signature omitted ] 

Message 6 in thread

On 12.12.06 16:13:24, joe lawrence wrote:
> I am writing a program in QT which must listen for data on multiple UDP
> sockets.  The program crashed unexpectedly whenever both sockets were
> actively receiving data.  The large program uses two separate event-loops,
> but a smaller, non-GUI test program I wrote has the same problem.  Here is
> the source code:

> A separate program sends small bits of data on ports 22222 and 33333 at
> about 2,000 packets/s (4 bytes of data -- the UDP packet size is clearly
> larger).  The original program receives larger packets at a similar rate (26
> bytes of data).

You should include that program (or better a stripped down version) too,
because else nobody can reproduce the problem

> The program runs for a little while and then shuts down after usually
> 10s-1min.  Here is the debug output:

Well, thats probably from your original program, a backtrace from your
example would have been better I think. My first guess would be the cout
line, because you're not protecting it with a mutex and thus both
threads might write to it at once, which is not really good, right?

Andreas

-- 
 [ signature omitted ] 

Message 7 in thread

joe lawrence wrote:
> The program runs for a little while and then shuts down after usually
> 10s-1min.  Here is the debug output:
>
> #---------------------------------------------------------------
> Program received signal SIGSEGV, Segmentation fault.
> [Switching to Thread -1210799184 (LWP 4260)]
> 0x48d42407 in g_slist_prepend () from /usr/lib/libglib-2.0.so.0
> (gdb) info thread
>   3 Thread -1219191888 (LWP 4261)  0x48d4279c in g_slist_remove ()
> from /usr/lib/libglib- 2.0.so.0
> * 2 Thread -1210799184 (LWP 4260)  0x48d42407 in g_slist_prepend ()
> from /usr/lib/libglib-2.0.so.0
>   1 Thread -1208699200 (LWP 4257)  0xb7f6a410 in ?? ()
> (gdb) backtrace
> #0  0x48d42407 in g_slist_prepend () from /usr/lib/libglib- 2.0.so.0
> #1  0x48d2e9cc in g_source_add_poll () from /usr/lib/libglib-2.0.so.0
> #2  0x48fe490d in QEventDispatcherGlib::registerSocketNotifier () from
> /usr/lib/libQtCore.so.4
> #3  0x48fd9e8c in QSocketNotifier::setEnabled () from
> /usr/lib/libQtCore.so.4
> #4  0x490a0953 in QNativeSocketEngine::setReadNotificationEnabled ()
> from /usr/lib/libQtNetwork.so.4
> #5  0x490accb6 in QUdpSocket::readDatagram () from
> /usr/lib/libQtNetwork.so.4
> #6  0x0804917c in Thread::run (this=0xbf9dbba0) at qtbreakhost.cpp:26
> #7  0x48f2fff0 in QThreadPrivate::start () from /usr/lib/libQtCore.so.4
> #8  0x48bf4371 in start_thread () from /lib/tls/libpthread.so.0
> #9  0x48a5bffe in clone () from /lib/tls/libc.so.6
> #---------------------------------------------------------------
>
> As you can see, it crashes on a glib linked-list operation.  Any ideas?

I've run into a mysterious glib crash in a multi-threaded Qt4 app a
month or two ago as well. My suggestion is that you rebuild Qt with
-no-glib switch -- that fixed the problem for me.

    Paul.

--
 [ signature omitted ] 

Message 8 in thread

On Tuesday 12 December 2006 22:13, joe lawrence wrote:
> I am writing a program in QT which must listen for data on multiple UDP
> sockets.  The program crashed unexpectedly whenever both sockets were
> actively receiving data.  The large program uses two separate event-loops,
> but a smaller, non-GUI test program I wrote has the same problem.  Here is
> the source code:
>
[snip]
> A separate program sends small bits of data on ports 22222 and 33333 at
> about 2,000 packets/s (4 bytes of data -- the UDP packet size is clearly
> larger).  The original program receives larger packets at a similar rate
> (26 bytes of data).
>
> The program runs for a little while and then shuts down after usually
> 10s-1min.  Here is the debug output:
>
[snip]
> #1  0x48d2e9cc in g_source_add_poll () from /usr/lib/libglib-2.0.so.0
> #2  0x48fe490d in QEventDispatcherGlib::registerSocketNotifier () from
> /usr/lib/libQtCore.so.4
[snip]
>
> As you can see, it crashes on a glib linked-list operation.  Any ideas?

That looks like a problem that we need to debug. Have you reported to 
qt-bugs@xxxxxxxxxxxxx? If not, could you please? :)

> thanks,
> joe

-- 
 [ signature omitted ] 

Message 9 in thread

I have reported this to Trolltech.  I will try Paul's suggestion and rebuild
QT without glib.  Thanks so much!

 - Joe

On 12/13/06, Bradley T Hughes <bhughes@xxxxxxxxxxxxx> wrote:
>
> On Tuesday 12 December 2006 22:13, joe lawrence wrote:
> > I am writing a program in QT which must listen for data on multiple UDP
> > sockets.  The program crashed unexpectedly whenever both sockets were
> > actively receiving data.  The large program uses two separate
> event-loops,
> > but a smaller, non-GUI test program I wrote has the same problem.  Here
> is
> > the source code:
> >
> [snip]
> > A separate program sends small bits of data on ports 22222 and 33333 at
> > about 2,000 packets/s (4 bytes of data -- the UDP packet size is clearly
> > larger).  The original program receives larger packets at a similar rate
> > (26 bytes of data).
> >
> > The program runs for a little while and then shuts down after usually
> > 10s-1min.  Here is the debug output:
> >
> [snip]
> > #1  0x48d2e9cc in g_source_add_poll () from /usr/lib/libglib-2.0.so.0
> > #2  0x48fe490d in QEventDispatcherGlib::registerSocketNotifier () from
> > /usr/lib/libQtCore.so.4
> [snip]
> >
> > As you can see, it crashes on a glib linked-list operation.  Any ideas?
>
> That looks like a problem that we need to debug. Have you reported to
> qt-bugs@xxxxxxxxxxxxx? If not, could you please? :)
>
> > thanks,
> > joe
>
> --
> Bradley T. Hughes - bhughes at trolltech.com
> Trolltech ASA - Sandakervn. 116, P.O. Box 4332 Nydalen, 0402 Oslo, Norway
>