Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Writing to QDataStream causes segmentation fault
Forum Update on Monday, May 27th 2025

[SOLVED] Writing to QDataStream causes segmentation fault

Scheduled Pinned Locked Moved General and Desktop
22 Posts 6 Posters 10.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    butterface
    wrote on 30 Sept 2012, 21:22 last edited by
    #1

    Hi,

    I have a class A which subclasses QWidget and a class B which subclasses class A. Both of them implement mousePressEvent as follows:

    @
    void A::mousePressEvent(QMouseEvent *event)
    {
    if (event->button() == Qt::LeftButton)
    dragStartPosition = event->pos();
    QByteArray itemData;
    QBuffer *b = new QBuffer(&itemData);
    b->open(QIODevice::WriteOnly);
    dataStream.setDevice(b);
    dataStream << QPoint(event->pos());
    QMimeData *mimeData = new QMimeData;
    mimeData->setData("application/x-dnditemdata", itemData);
    drag = new QDrag(this);
    drag->setMimeData(mimeData);
    drag->setPixmap(QPixmap::grabWidget(this));
    drag->setHotSpot(event->pos());
    }
    @

    and the method of class B

    @
    void B::mousePressEvent(QMouseEvent *event)
    {
    A::mousePressEvent(event);
    dataStream << 25;
    if (acceptDrops())
    drag->exec(Qt::MoveAction);
    else
    drag->exec(Qt::CopyAction);
    }
    @

    At dataStream << 25; I get a segmentation fault even though the reference seems to be valid and getting the status of the stream returns 0 (OK). The datastream is am member of A. Can anyone please tell me what I am missing?

    Thanks

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on 1 Oct 2012, 08:51 last edited by
      #2

      Where does dataStream come from?
      Also, I think it is dangerous to just stream in '25'. Do you know what exactly is streamed in here? I'd always make sure to use an explicit type:
      @
      quint16 value(25);
      dataStream << value;
      @
      Now, I am sure that I have a quint16 in the stream, not an integer of a who-knows-what type that depends on the platform the code was compiled on.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        butterface
        wrote on 1 Oct 2012, 12:35 last edited by
        #3

        @dataStream@ is a member of A. As long as I am in A I can write to it without any problems. The 25 is just a test value. It has no meaning at all and in my program der is a method call which returns an int. I can use quint16 later but for writing it should not make a difference, should it?

        1 Reply Last reply
        0
        • B Offline
          B Offline
          broadpeak
          wrote on 1 Oct 2012, 12:48 last edited by
          #4

          How can B sees A's dataStream ? Not clear for me...

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nikCoder
            wrote on 1 Oct 2012, 13:19 last edited by
            #5

            "QByteArray itemData" in void A::mousePressEvent(QMouseEvent *event) is a local, Hence the segfault . Allocate on heap or as a member . Hope it helps.

            Cheers,
            Nikhil

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on 1 Oct 2012, 15:11 last edited by
              #6

              Also, I believe that the
              @
              QBuffer *b = new QBuffer(&itemData);
              @ creates a memory leak.

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              0
              • B Offline
                B Offline
                butterface
                wrote on 1 Oct 2012, 17:34 last edited by
                #7

                Thanks Nikhil, it solved the problem. One day I will learn this stupid heap and stack thing.

                @mlong: Why? Now itemData is a member as well. I should be able to delete it but I cannot see the memory leak of the implementation before.

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  nikCoder
                  wrote on 1 Oct 2012, 17:54 last edited by
                  #8

                  I agree with mlong, there is lot of leaks in your code. Unless required dont allocate memory on heap (i.e using new) . Instead , make QBuffer object an member variable. If you need to use smart pointers when allocating. Every new , must have a delete , else you have potential memory leaks. In case of @ QBuffer *b = new QBuffer(&itemData);@ if b is not an member of the class , then in destructor you cant delete it since, b is a local pointer .

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mlong
                    wrote on 1 Oct 2012, 17:56 last edited by
                    #9

                    b is allocated with a new() but never deleted anywhere that I can see. (Unless something takes ownership of the buffer object and deletes it on your behalf, but I don't think it does.) I know that there's no parent object given to the buffer.

                    Software Engineer
                    My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      butterface
                      wrote on 2 Oct 2012, 06:09 last edited by
                      #10

                      Thanks for your help so far. I changed those objects to references or added deletes where necessary. It works now, at least some kind of.
                      I can read some information from the stream but something seems to be wrong. The QPoint I added is restored as expected in the dropEvent but my own object is not. For my class to write into the stream I have operator>> and operator<< as non-members. At the moment I only add an int which I cast(?) to quint32 as Andre mentioned but on the other end, when I try to read it again it is always 0. I don't get any errors so something valid must be there but not what I expect.
                      Any ideas?

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        goetz
                        wrote on 2 Oct 2012, 11:58 last edited by
                        #11

                        You should start to debug your operators without drag/drop functionality.

                        Just create an object, call the output operator. Then destroy your object; create a new one and call it on your input operator working on the data you have written previously. Don't forget to close the file and re-open it or reset the file pointer to the beginning. Then use a debugger to see what's coming in from the stream. Also, have a look at the file, e.g. by using some hexdump utility. The data is usually pretty straight forward to interpret.

                        Once you're sure that this all works, go back to the DnD thingie.

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        1 Reply Last reply
                        0
                        • B Offline
                          B Offline
                          butterface
                          wrote on 2 Oct 2012, 21:32 last edited by
                          #12

                          I just figured out that everything works fine (read and write) if I write in the base class A. Everything written in B could not be read again.

                          Here again both methods:

                          @
                          void A::mousePressEvent(QMouseEvent *event)
                          {
                          if (event->button() == Qt::LeftButton)
                          dragStartPosition = event->pos();
                          itemData = new QByteArray();
                          b = new QBuffer(itemData);
                          b->open(QIODevice::WriteOnly);
                          dataStream.setDevice(b);
                          dataStream << QPoint(event->pos());
                          mimeData = new QMimeData;
                          mimeData->setData("application/x-dnditemdata", *itemData);
                          drag = new QDrag(this);
                          drag->setMimeData(mimeData);
                          drag->setPixmap(QPixmap::grabWidget(this));
                          drag->setHotSpot(event->pos());
                          }
                          @

                          @
                          void B::mousePressEvent(QMouseEvent *event)
                          {
                          A::mousePressEvent(event);
                          quint16 val(4);
                          dataStream << val; //this one can never be read out again...
                          if (acceptDrops())
                          drag->exec(Qt::MoveAction);
                          else
                          drag->exec(Qt::CopyAction);
                          }
                          @

                          @
                          class A : public QWidget
                          {
                          public:
                          A(QWidget *parent = NULL, bool acceptDrops = true);
                          virtual ~A();

                          protected:
                          virtual void mousePressEvent(QMouseEvent *);

                          QPoint dragStartPosition;
                          QDrag *drag;
                          QDataStream dataStream;
                          QByteArray *itemData;
                          QBuffer *b;
                          QMimeData *mimeData;
                          

                          };
                          @

                          1 Reply Last reply
                          0
                          • B Offline
                            B Offline
                            butterface
                            wrote on 3 Oct 2012, 06:45 last edited by
                            #13

                            The "funny" thing is if I step through with the debugger the value is written to the stream in the subclass but if the program runs free it is not. I can see the qDebug message right before the write to the datastream in any case but the data is only writte in debug mode?!

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              andre
                              wrote on 3 Oct 2012, 08:19 last edited by
                              #14

                              Well, so far you are still not showing us how class A and B are related, so it is very hard to get an idea of what is actually going on in your code.

                              1 Reply Last reply
                              0
                              • B Offline
                                B Offline
                                butterface
                                wrote on 3 Oct 2012, 10:39 last edited by
                                #15

                                Sorry for asking, but is there another relation between them then subclassing, i. e. B is a subclass of A.
                                I can post the exact class definition when I am back home.

                                1 Reply Last reply
                                0
                                • B Offline
                                  B Offline
                                  butterface
                                  wrote on 3 Oct 2012, 20:50 last edited by
                                  #16

                                  And here comes definition of class B

                                  @
                                  class B : public A
                                  {
                                  Q_OBJECT
                                  public:
                                  B(QWidget *parent = NULL, bool acceptDrops = true);
                                  virtual ~B();

                                  protected:
                                  void paintEvent(QPaintEvent *);
                                  void mousePressEvent(QMouseEvent *);
                                  };
                                  @

                                  1 Reply Last reply
                                  0
                                  • G Offline
                                    G Offline
                                    goetz
                                    wrote on 3 Oct 2012, 20:59 last edited by
                                    #17

                                    In class A's mousePressEvent function, you set the mime data in the QDrag object in line 11:

                                    @
                                    mimeData->setData("application/x-dnditemdata", *itemData);
                                    @

                                    At this point in time, only the QPoint data has been written to the QDataStream and its underlying byte array.

                                    Once you come back to class B's mousePressEvent function, you write some more data to the byte array, but that never reaches the QDrag object! QDrag takes a copy of the data. At the moment you write to the array in class B, the implicit sharing creates a deep copy of the array and in turn, you end up with two QByteArrays: one that's within the drag object, and one in class B, which is never used again.

                                    As a side note: Your split of code looks very strange to me and in my opinion it cries for additional trouble in the future.

                                    http://www.catb.org/~esr/faqs/smart-questions.html

                                    1 Reply Last reply
                                    0
                                    • B Offline
                                      B Offline
                                      butterface
                                      wrote on 3 Oct 2012, 21:14 last edited by
                                      #18

                                      Okay, thanks for that so far.

                                      What would you suggest for the setting of the QByteArray? The point is, I want to make sure that subclasses don't need to take care about those things. I want the base class to take care of the clicked points and all the things which come again and again (like the definition of when I make a base class). If there is no other way, I can move the whole dnd code to the subclasses of course.
                                      What do you mean by "it looks strange"? I actually took the dnd example from the website and changed a few things like adding my own object.

                                      So please let me know how to make it better and what you would suggest for the problem of the subclass datastream writing.

                                      1 Reply Last reply
                                      0
                                      • G Offline
                                        G Offline
                                        goetz
                                        wrote on 3 Oct 2012, 21:30 last edited by
                                        #19

                                        I would split out the preparation of the mime data into some other virtual functions. Only implement the mousepress event handler in class A and call the new functions from that. Basicall like this:

                                        @
                                        void A::mousePressEvent(QMouseEvent *event)
                                        {
                                        QByteArray itemData;
                                        QDataStream dataStream(&itemData, QIODevice::WriteOnly);

                                        // the new function:
                                        prepareDragData(event, dataStream);
                                        
                                        QMimeData *mimeData = new QMimeData;
                                        mimeData->setData("application/x-dnditemdata", itemData);
                                        drag = new QDrag(this);
                                        drag->setMimeData(mimeData);
                                        drag->setPixmap(QPixmap::grabWidget(this));
                                        drag->setHotSpot(event->pos());
                                        
                                        if (acceptDrops())
                                            drag->exec&#40;Qt::MoveAction&#41;;
                                        else
                                            drag->exec&#40;Qt::CopyAction&#41;;
                                        

                                        }

                                        void A::prepareDragData(QMouseEvent *event, QDataStream &stream)
                                        {
                                        dataStream << QPoint(event->pos());
                                        }

                                        void B::prepareDragData(QMouseEvent *event, QDataStream &stream)
                                        {
                                        A::prepareDragData(event, stream);
                                        dataStream << 25;
                                        }
                                        @

                                        Additional notes:

                                        • no more need of class variables for the dataStream
                                        • no need for a QBuffer, the dataStream can work directly on a QByteArray

                                        http://www.catb.org/~esr/faqs/smart-questions.html

                                        1 Reply Last reply
                                        0
                                        • A Offline
                                          A Offline
                                          andre
                                          wrote on 4 Oct 2012, 07:52 last edited by
                                          #20

                                          I think Volkers suggestion makes a lot of sense. However, perhaps you should even considder taking it a step further. Does your drag target really need to know the QPoint where the drag started? Or is it really only interested in some kind of handle to the object that is being dragged, and the type of that object? Usually, in my experience, it is the latter...

                                          1 Reply Last reply
                                          0

                                          1/22

                                          30 Sept 2012, 21:22

                                          • Login

                                          • Login or register to search.
                                          1 out of 22
                                          • First post
                                            1/22
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved