Results 1 to 10 of 10

Thread: QTcpSocket crashes on read

  1. #1
    Join Date
    Aug 2009
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTcpSocket crashes on read

    Hi,

    I'm not sure if this is a QT bug or something of my own doing, but the qbytearray that acts as the receive buffer for QTcpSocket crashes while performing a memcpy (called by resize)

    The only socket read I perform is in the following slot code:
    void DataFeed::readData(void)

    { //cout << "Reading data from device in slot..." << endl; QByteArray *pData = new QByteArray; *pData = mp_socket->readAll(); emit postData(pData); }
    Here is my connection to the slot:

    connect(mp_socket, SIGNAL(readyRead()), this, SLOT(readData()));

    Here is my stack trace, from what I can tell the error is not generated because of my code or is related to the QByteArray I create on the heap.
    0 memcpy /lib/x86_64-linux-gnu/libc.so.6 0 0x00007ffff66a6abb
    1 QByteArray::realloc qbytearray.cpp 1424 0x00007ffff73fd56b
    2 QByteArray::resize qbytearray.cpp 1389 0x00007ffff73fd9a9
    3 QRingBuffer::reserve qringbuffer_p.h 158 0x00007ffff791e6df
    4 QAbstractSocketPrivate::readFromSocket qabstractsocket.cpp 1158 0x00007ffff791912f
    5 QAbstractSocketPrivate::canReadNotification qabstractsocket.cpp 614 0x00007ffff79194f5
    6 QReadNotifier::event qnativesocketengine.cpp 1103 0x00007ffff7908791
    7 QCoreApplicationPrivate::notify_helper qcoreapplication.cpp 867 0x00007ffff75024cf
    8 QCoreApplication::notify qcoreapplication.cpp 813 0x00007ffff750254e
    9 QCoreApplication::notifyInternal qcoreapplication.cpp 732 0x00007ffff75020a4
    10 sendEvent qcoreapplication.h 215 0x00007ffff752fa3a
    11 socketNotifierSourceDispatch qeventdispatcher_glib.cpp 110 0x00007ffff752fa3a
    12 g_main_context_dispatch /lib/x86_64-linux-gnu/libglib-2.0.so.0 0 0x00007ffff5d5ebcd
    13 ?? /lib/x86_64-linux-gnu/libglib-2.0.so.0 0 0x00007ffff5d5f3a8
    14 g_main_context_iteration /lib/x86_64-linux-gnu/libglib-2.0.so.0 0 0x00007ffff5d5f639
    15 QEventDispatcherGlib:rocessEvents qeventdispatcher_glib.cpp 415 0x00007ffff752fbcc
    16 QEventLoop:rocessEvents qeventloop.cpp 149 0x00007ffff75013f5
    17 QEventLoop::exec qeventloop.cpp 201 0x00007ffff7501646
    18 QThread::exec qthread.cpp 490 0x00007ffff73f8014
    19 DataFeed::run datafeed.cpp 103 0x000000000040c477
    20 QThreadPrivate::start qthread_unix.cpp 266 0x00007ffff73faf5f
    21 start_thread /lib/x86_64-linux-gnu/libpthread.so.0 0 0x00007ffff7157d8c
    22 clone /lib/x86_64-linux-gnu/libc.so.6 0 0x00007ffff670204d
    23 ?? 0 0x0000000000000000

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket crashes on read

    Why all the complexity? What wrong with just "postData(mp_socket->readAll());" ? All you are doing is creating memory leaks...

  3. #3
    Join Date
    Aug 2009
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket crashes on read

    I was trying to avoid the deep copy that would be necessary for a possible queued connection, but I agree with your point. In my defense, I do track the pointer and reclaim the memory...which I originally thought was related to the problem. The trace does not make me believe that though.

    Any ideas on the problem?

    Thanks

  4. #4
    Join Date
    Aug 2009
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket crashes on read

    Just for kicks I changed the slot to your recommendation and I still get the exact same crash.

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket crashes on read

    Are you sure that mp_socket is a valid pointer at all times? Add an assert to ensure.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket crashes on read

    Are you using threads?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Aug 2009
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket crashes on read

    Quote Originally Posted by wysota View Post
    Are you using threads?
    Yes I am, am I doing something wrong?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket crashes on read

    Quote Originally Posted by xenome View Post
    Yes I am, am I doing something wrong?
    Possibly. How do you use them?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Aug 2009
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket crashes on read

    I have my network interface thread that created the socket and posts the data as shown in the above slot code. I also have a message parser thread which takes in this QByteArray from the signal and adds the data to its internal buffer for parsing. This thread spins in an infinite loop reading tokens from this buffer (semaphore protected) and once it recognizes a complete message, it posts a new message (msg ptr to new message on the heap), this gets caught by another slot which adds the message (msg ptr) to a thread safe queue. The address of the queue is then sent to another thread for processing, once processing is complete the msg ptr is deleted.

    Sounds a lot more complicated than it really is.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket crashes on read

    What do you need all those threads for? Sounds like one thread would just be enough to do the same task (and probably without crashing your application).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QtcpSocket read behavior
    By grisson in forum Qt Programming
    Replies: 5
    Last Post: 14th August 2010, 16:31
  2. My server (using QTcpServer and QTcpSocket) crashes
    By supergillis in forum Qt Programming
    Replies: 3
    Last Post: 2nd June 2010, 15:32
  3. read and write on qtcpsocket
    By dognzhe in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 09:42
  4. Unable to read from QTcpSocket
    By jimroos in forum Qt Programming
    Replies: 1
    Last Post: 4th July 2007, 21:09
  5. How to read more bytes using QTcpSocket?
    By vishesh in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2007, 20:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.