Convert to and from QDataStream into struct of arrays results in sigfault?

  • Hi All,
    I would like to let two Qt programs exchange information via the network. Therefore I would like to encode the data on the transmitter side into a QDataStream, and decode it on the receiver side. I am now trying to get the encoding and decoding part correct, the next step is to use the QDataStreams like in the network/fortune example. I've made the following program, but the data is not decoded correctly, and I see a segmentation fault during the last for loop. I suspect the problem is between the chair and the keyboard, but I don't see it yet :-) Cheers,

    #ifndef OVERIDE_H
    #define OVERIDE_H
    #include <QObject>
    #include <QDataStream>
    #define NUMLIGHTS 64
    #define STREAMVERSION QDataStream::Qt_4_0
    struct msgOveride
        qint8 overridden[NUMLIGHTS];
        float lightvalue[NUMLIGHTS];
    class overide : public QObject
        explicit overide(QObject *parent = 0);
    public slots:
        msgOveride myOverrides;
        msgOveride myRxOverrides;
        qint16 myBlocksizeRx;
    #endif // OVERIDE_H
    #include "overide.h"
    #include <QByteArray>
    overide::overide(QObject *parent) : QObject(parent)
        for (int i = 0; i < NUMLIGHTS; ++i)
        QByteArray block;
        QDataStream out(&block, QIODevice::WriteOnly);
        out << (quint16)0;
        for (int i = 0; i < NUMLIGHTS; ++i)
            out << myOverrides.lightvalue[i];
            out << myOverrides.overridden[i];
        out << (quint16)(block.size() - sizeof(quint16));
        QDataStream in(&block, QIODevice::ReadOnly);
        //emulate transmission via network:
        //now consume the datastream at the receiving end
        in >> myBlocksizeRx;
        //check rx size here
        for (int i = 0; i < NUMLIGHTS; ++i)
            in >> myRxOverrides.lightvalue[i];  //segfault when i=56, and value[3]and [54] is incorrect
            in >> myRxOverrides.overridden[i];

  • I forgot to give my version numbers:
    Windows 7 enterprise service pack 1, 64 bits
    Qt Creator 3.6.0
    Based on Qt 5.5.1 (MSVC 2013, 32 bit)
    Built on Dec 15 2015 01:01:38
    From revision b52c2f91f5
    C:/Qt/Qt5.5/Tools/mingw492_32/bin/mingw32-make -f Makefile.Debug

    Changing this line still generates a segfault:
    #define STREAMVERSION QDataStream::Qt_DefaultCompiledVersion


  • Hi All,
    I have also tried it in a virtual machine running linux mint. There it works without any problems.
    The version of Qt is a bit older, so I had to change this line:
    #define STREAMVERSION QDataStream::Qt_4_8
    My versions:
    $ uname -a
    Linux cedric-VirtualBox 3.19.0-32-generic #37~14.04.1-Ubuntu SMP Thu Oct 22 09:41:40 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
    Qt Creator 3.0.1
    Based on Qt 5.2.1 (GCC 4.8.2, 64 bit)
    Built on Apr 9 2014 at 09:12:59
    g++ -c -m64 -pipe -g -Wall -W -D_REENTRANT -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++-64 -I../overide -I/usr/include/qt4/QtCore -I/usr/include/qt4 -I. -I../overide -I. -o main.o ../overide/main.cpp

    I have also tried streamversion 4.8 in windows, but it still segfaults. This tells me there is something wrong with the Qt version (or compiler) I use on windows. I will try to update Qt on both platforms, and see what happens.


  • is myBlocksizeRx 320 (it should)?
    did you try uncomment //in.device()->seek(0);?

  • Hi Vronin,
    myBlocksizeRx is 576 bytes. As far as I know it should be (1 byte per qint8 + 8 bytes per float)*64 = 576 bytes.

    But it's very strange, I have done the following steps, and now the problem does not occur anymore. I don't like this, as I have not adjusted my code, so I cannot be sure the problem does not re-appear when I expand my code.
    -> rename c:\Qt to C:\Qt-moved
    ->install the latest version of Qt (5.7) in c:\Qt
    ->try to build the application with Qt 5.7. (did not work, as I don't have a debugger out-of-the-box)
    ->rename c:\Qt into Qt-new (containing Qt5.7)
    ->rename C:\qt-moved into c:\qt (containing Qt 5.5)
    ->remove the build folder and the .user file of my program
    ->build and debug the program.
    Now the program works without problems.

    For now the problem is gone, but I hope it does not return. Thanks for the help,

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.