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 Updated to NodeBB v4.3 + New Features

[SOLVED] Writing to QDataStream causes segmentation fault

Scheduled Pinned Locked Moved General and Desktop
22 Posts 6 Posters 10.4k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    mlong
    wrote on 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 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 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 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 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 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 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 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 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 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 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 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
                          • B Offline
                            B Offline
                            butterface
                            wrote on last edited by
                            #21

                            Andre, I am actually not really interested in this specific point. It was just the implementation from the example but there will be other data which I really need.

                            1 Reply Last reply
                            0
                            • B Offline
                              B Offline
                              butterface
                              wrote on last edited by
                              #22

                              FYI: I changed the whole thing now to use QGraphicsScene which is much easier to use and dose all the d'n'd stuff and mouse handling by itself.

                              1 Reply Last reply
                              0

                              • Login

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