<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[SOLVED] Writing to QDataStream causes segmentation fault]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I have a class A which subclasses QWidget and a class B which subclasses class A. Both of them implement mousePressEvent as follows:</p>
<p dir="auto">@<br />
void A::mousePressEvent(QMouseEvent *event)<br />
{<br />
if (event-&gt;button() == Qt::LeftButton)<br />
dragStartPosition = event-&gt;pos();<br />
QByteArray itemData;<br />
QBuffer *b = new QBuffer(&amp;itemData);<br />
b-&gt;open(QIODevice::WriteOnly);<br />
dataStream.setDevice(b);<br />
dataStream &lt;&lt; QPoint(event-&gt;pos());<br />
QMimeData *mimeData = new QMimeData;<br />
mimeData-&gt;setData("application/x-dnditemdata", itemData);<br />
drag = new QDrag(this);<br />
drag-&gt;setMimeData(mimeData);<br />
drag-&gt;setPixmap(QPixmap::grabWidget(this));<br />
drag-&gt;setHotSpot(event-&gt;pos());<br />
}<br />
@</p>
<p dir="auto">and the method of class B</p>
<p dir="auto">@<br />
void B::mousePressEvent(QMouseEvent *event)<br />
{<br />
A::mousePressEvent(event);<br />
dataStream &lt;&lt; 25;<br />
if (acceptDrops())<br />
drag-&gt;exec(Qt::MoveAction);<br />
else<br />
drag-&gt;exec(Qt::CopyAction);<br />
}<br />
@</p>
<p dir="auto">At dataStream &lt;&lt; 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?</p>
<p dir="auto">Thanks</p>
]]></description><link>https://forum.qt.io/topic/20265/solved-writing-to-qdatastream-causes-segmentation-fault</link><generator>RSS for Node</generator><lastBuildDate>Tue, 18 Aug 2026 02:40:52 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/20265.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 30 Sep 2012 21:22:43 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [SOLVED] Writing to QDataStream causes segmentation fault on Sun, 14 Oct 2012 10:34:28 GMT]]></title><description><![CDATA[<p dir="auto">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.</p>
]]></description><link>https://forum.qt.io/post/154452</link><guid isPermaLink="true">https://forum.qt.io/post/154452</guid><dc:creator><![CDATA[butterface]]></dc:creator><pubDate>Sun, 14 Oct 2012 10:34:28 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Writing to QDataStream causes segmentation fault on Thu, 11 Oct 2012 19:43:55 GMT]]></title><description><![CDATA[<p dir="auto">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.</p>
]]></description><link>https://forum.qt.io/post/154293</link><guid isPermaLink="true">https://forum.qt.io/post/154293</guid><dc:creator><![CDATA[butterface]]></dc:creator><pubDate>Thu, 11 Oct 2012 19:43:55 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Writing to QDataStream causes segmentation fault on Thu, 04 Oct 2012 07:52:52 GMT]]></title><description><![CDATA[<p dir="auto">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...</p>
]]></description><link>https://forum.qt.io/post/153625</link><guid isPermaLink="true">https://forum.qt.io/post/153625</guid><dc:creator><![CDATA[andre]]></dc:creator><pubDate>Thu, 04 Oct 2012 07:52:52 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Writing to QDataStream causes segmentation fault on Wed, 03 Oct 2012 21:30:13 GMT]]></title><description><![CDATA[<p dir="auto">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:</p>
<p dir="auto">@<br />
void A::mousePressEvent(QMouseEvent *event)<br />
{<br />
QByteArray itemData;<br />
QDataStream dataStream(&amp;itemData, QIODevice::WriteOnly);</p>
<pre><code>// the new function:
prepareDragData(event, dataStream);

QMimeData *mimeData = new QMimeData;
mimeData-&gt;setData("application/x-dnditemdata", itemData);
drag = new QDrag(this);
drag-&gt;setMimeData(mimeData);
drag-&gt;setPixmap(QPixmap::grabWidget(this));
drag-&gt;setHotSpot(event-&gt;pos());

if (acceptDrops())
    drag-&gt;exec&amp;#40;Qt::MoveAction&amp;#41;;
else
    drag-&gt;exec&amp;#40;Qt::CopyAction&amp;#41;;
</code></pre>
<p dir="auto">}</p>
<p dir="auto">void A::prepareDragData(QMouseEvent *event, QDataStream &amp;stream)<br />
{<br />
dataStream &lt;&lt; QPoint(event-&gt;pos());<br />
}</p>
<p dir="auto">void B::prepareDragData(QMouseEvent *event, QDataStream &amp;stream)<br />
{<br />
A::prepareDragData(event, stream);<br />
dataStream &lt;&lt; 25;<br />
}<br />
@</p>
<p dir="auto">Additional notes:</p>
<ul>
<li>no more need of class variables for the dataStream</li>
<li>no need for a QBuffer, the dataStream can work directly on a QByteArray</li>
</ul>
]]></description><link>https://forum.qt.io/post/153597</link><guid isPermaLink="true">https://forum.qt.io/post/153597</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Wed, 03 Oct 2012 21:30:13 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Writing to QDataStream causes segmentation fault on Wed, 03 Oct 2012 21:14:25 GMT]]></title><description><![CDATA[<p dir="auto">Okay, thanks for that so far.</p>
<p dir="auto">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.<br />
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.</p>
<p dir="auto">So please let me know how to make it better and what you would suggest for the problem of the subclass datastream writing.</p>
]]></description><link>https://forum.qt.io/post/153595</link><guid isPermaLink="true">https://forum.qt.io/post/153595</guid><dc:creator><![CDATA[butterface]]></dc:creator><pubDate>Wed, 03 Oct 2012 21:14:25 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Writing to QDataStream causes segmentation fault on Wed, 03 Oct 2012 20:59:09 GMT]]></title><description><![CDATA[<p dir="auto">In class A's mousePressEvent function, you set the mime data in the QDrag object in line 11:</p>
<p dir="auto">@<br />
mimeData-&gt;setData("application/x-dnditemdata", *itemData);<br />
@</p>
<p dir="auto">At this point in time, only the QPoint data has been written to the QDataStream and its underlying byte array.</p>
<p dir="auto">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 <em>copy</em> 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 <em>two</em> QByteArrays: one that's within the drag object, and one in class B, which is never used again.</p>
<p dir="auto">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.</p>
]]></description><link>https://forum.qt.io/post/153593</link><guid isPermaLink="true">https://forum.qt.io/post/153593</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Wed, 03 Oct 2012 20:59:09 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Writing to QDataStream causes segmentation fault on Wed, 03 Oct 2012 20:50:37 GMT]]></title><description><![CDATA[<p dir="auto">And here comes definition of class B</p>
<p dir="auto">@<br />
class B : public A<br />
{<br />
Q_OBJECT<br />
public:<br />
B(QWidget *parent = NULL, bool acceptDrops = true);<br />
virtual ~B();</p>
<p dir="auto">protected:<br />
void paintEvent(QPaintEvent *);<br />
void mousePressEvent(QMouseEvent *);<br />
};<br />
@</p>
]]></description><link>https://forum.qt.io/post/153592</link><guid isPermaLink="true">https://forum.qt.io/post/153592</guid><dc:creator><![CDATA[butterface]]></dc:creator><pubDate>Wed, 03 Oct 2012 20:50:37 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Writing to QDataStream causes segmentation fault on Wed, 03 Oct 2012 10:39:40 GMT]]></title><description><![CDATA[<p dir="auto">Sorry for asking, but is there another relation between them then subclassing, i. e. B is a subclass of A.<br />
I can post the exact class definition when I am back home.</p>
]]></description><link>https://forum.qt.io/post/153569</link><guid isPermaLink="true">https://forum.qt.io/post/153569</guid><dc:creator><![CDATA[butterface]]></dc:creator><pubDate>Wed, 03 Oct 2012 10:39:40 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Writing to QDataStream causes segmentation fault on Wed, 03 Oct 2012 08:19:26 GMT]]></title><description><![CDATA[<p dir="auto">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.</p>
]]></description><link>https://forum.qt.io/post/153549</link><guid isPermaLink="true">https://forum.qt.io/post/153549</guid><dc:creator><![CDATA[andre]]></dc:creator><pubDate>Wed, 03 Oct 2012 08:19:26 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Writing to QDataStream causes segmentation fault on Wed, 03 Oct 2012 06:45:19 GMT]]></title><description><![CDATA[<p dir="auto">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?!</p>
]]></description><link>https://forum.qt.io/post/153543</link><guid isPermaLink="true">https://forum.qt.io/post/153543</guid><dc:creator><![CDATA[butterface]]></dc:creator><pubDate>Wed, 03 Oct 2012 06:45:19 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Writing to QDataStream causes segmentation fault on Tue, 02 Oct 2012 21:32:01 GMT]]></title><description><![CDATA[<p dir="auto">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.</p>
<p dir="auto">Here again both methods:</p>
<p dir="auto">@<br />
void A::mousePressEvent(QMouseEvent *event)<br />
{<br />
if (event-&gt;button() == Qt::LeftButton)<br />
dragStartPosition = event-&gt;pos();<br />
itemData = new QByteArray();<br />
b = new QBuffer(itemData);<br />
b-&gt;open(QIODevice::WriteOnly);<br />
dataStream.setDevice(b);<br />
dataStream &lt;&lt; QPoint(event-&gt;pos());<br />
mimeData = new QMimeData;<br />
mimeData-&gt;setData("application/x-dnditemdata", *itemData);<br />
drag = new QDrag(this);<br />
drag-&gt;setMimeData(mimeData);<br />
drag-&gt;setPixmap(QPixmap::grabWidget(this));<br />
drag-&gt;setHotSpot(event-&gt;pos());<br />
}<br />
@</p>
<p dir="auto">@<br />
void B::mousePressEvent(QMouseEvent *event)<br />
{<br />
A::mousePressEvent(event);<br />
quint16 val(4);<br />
dataStream &lt;&lt; val; //this one can never be read out again...<br />
if (acceptDrops())<br />
drag-&gt;exec(Qt::MoveAction);<br />
else<br />
drag-&gt;exec(Qt::CopyAction);<br />
}<br />
@</p>
<p dir="auto">@<br />
class A : public QWidget<br />
{<br />
public:<br />
A(QWidget *parent = NULL, bool acceptDrops = true);<br />
virtual ~A();</p>
<p dir="auto">protected:<br />
virtual void mousePressEvent(QMouseEvent *);</p>
<pre><code>QPoint dragStartPosition;
QDrag *drag;
QDataStream dataStream;
QByteArray *itemData;
QBuffer *b;
QMimeData *mimeData;
</code></pre>
<p dir="auto">};<br />
@</p>
]]></description><link>https://forum.qt.io/post/153536</link><guid isPermaLink="true">https://forum.qt.io/post/153536</guid><dc:creator><![CDATA[butterface]]></dc:creator><pubDate>Tue, 02 Oct 2012 21:32:01 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Writing to QDataStream causes segmentation fault on Tue, 02 Oct 2012 11:58:31 GMT]]></title><description><![CDATA[<p dir="auto">You should start to debug your operators without drag/drop functionality.</p>
<p dir="auto">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.</p>
<p dir="auto">Once you're sure that this all works, go back to the DnD thingie.</p>
]]></description><link>https://forum.qt.io/post/153504</link><guid isPermaLink="true">https://forum.qt.io/post/153504</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Tue, 02 Oct 2012 11:58:31 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Writing to QDataStream causes segmentation fault on Tue, 02 Oct 2012 06:09:36 GMT]]></title><description><![CDATA[<p dir="auto">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.<br />
I can read some information from the stream but something seems to be wrong. The <em>QPoint</em> I added is restored as expected in the <em>dropEvent</em> but my own object is not. For my class to write into the stream I have <em>operator&gt;&gt;</em> and <em>operator&lt;&lt;</em> 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.<br />
Any ideas?</p>
]]></description><link>https://forum.qt.io/post/153481</link><guid isPermaLink="true">https://forum.qt.io/post/153481</guid><dc:creator><![CDATA[butterface]]></dc:creator><pubDate>Tue, 02 Oct 2012 06:09:36 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Writing to QDataStream causes segmentation fault on Mon, 01 Oct 2012 17:56:48 GMT]]></title><description><![CDATA[<p dir="auto">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 <em>parent</em> object given to the buffer.</p>
]]></description><link>https://forum.qt.io/post/153459</link><guid isPermaLink="true">https://forum.qt.io/post/153459</guid><dc:creator><![CDATA[mlong]]></dc:creator><pubDate>Mon, 01 Oct 2012 17:56:48 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Writing to QDataStream causes segmentation fault on Mon, 01 Oct 2012 17:54:39 GMT]]></title><description><![CDATA[<p dir="auto">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(&amp;itemData);@ if b is not an member of the class , then in destructor you cant delete it since, b is a local pointer .</p>
]]></description><link>https://forum.qt.io/post/153461</link><guid isPermaLink="true">https://forum.qt.io/post/153461</guid><dc:creator><![CDATA[nikCoder]]></dc:creator><pubDate>Mon, 01 Oct 2012 17:54:39 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Writing to QDataStream causes segmentation fault on Mon, 01 Oct 2012 17:34:32 GMT]]></title><description><![CDATA[<p dir="auto">Thanks Nikhil, it solved the problem. One day I will learn this stupid heap and stack thing.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mlong">@<bdi>mlong</bdi></a>: 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.</p>
]]></description><link>https://forum.qt.io/post/153457</link><guid isPermaLink="true">https://forum.qt.io/post/153457</guid><dc:creator><![CDATA[butterface]]></dc:creator><pubDate>Mon, 01 Oct 2012 17:34:32 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Writing to QDataStream causes segmentation fault on Mon, 01 Oct 2012 15:11:29 GMT]]></title><description><![CDATA[<p dir="auto">Also, I believe that the<br />
@<br />
QBuffer *b = new QBuffer(&amp;itemData);<br />
@ creates a memory leak.</p>
]]></description><link>https://forum.qt.io/post/153444</link><guid isPermaLink="true">https://forum.qt.io/post/153444</guid><dc:creator><![CDATA[mlong]]></dc:creator><pubDate>Mon, 01 Oct 2012 15:11:29 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Writing to QDataStream causes segmentation fault on Mon, 01 Oct 2012 13:19:18 GMT]]></title><description><![CDATA[<p dir="auto">"QByteArray itemData" in void A::mousePressEvent(QMouseEvent *event) is a local, Hence the segfault . Allocate on heap or as a member . Hope it helps.</p>
<p dir="auto">Cheers,<br />
Nikhil</p>
]]></description><link>https://forum.qt.io/post/153433</link><guid isPermaLink="true">https://forum.qt.io/post/153433</guid><dc:creator><![CDATA[nikCoder]]></dc:creator><pubDate>Mon, 01 Oct 2012 13:19:18 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Writing to QDataStream causes segmentation fault on Mon, 01 Oct 2012 12:48:11 GMT]]></title><description><![CDATA[<p dir="auto">How can B sees A's dataStream ? Not clear for me...</p>
]]></description><link>https://forum.qt.io/post/153426</link><guid isPermaLink="true">https://forum.qt.io/post/153426</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Mon, 01 Oct 2012 12:48:11 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Writing to QDataStream causes segmentation fault on Mon, 01 Oct 2012 12:35:00 GMT]]></title><description><![CDATA[<p dir="auto">@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?</p>
]]></description><link>https://forum.qt.io/post/153424</link><guid isPermaLink="true">https://forum.qt.io/post/153424</guid><dc:creator><![CDATA[butterface]]></dc:creator><pubDate>Mon, 01 Oct 2012 12:35:00 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] Writing to QDataStream causes segmentation fault on Mon, 01 Oct 2012 08:51:19 GMT]]></title><description><![CDATA[<p dir="auto">Where does <em>dataStream</em> come from?<br />
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:<br />
@<br />
quint16 value(25);<br />
dataStream &lt;&lt; value;<br />
@<br />
Now, I am <em>sure</em> 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.</p>
]]></description><link>https://forum.qt.io/post/153378</link><guid isPermaLink="true">https://forum.qt.io/post/153378</guid><dc:creator><![CDATA[andre]]></dc:creator><pubDate>Mon, 01 Oct 2012 08:51:19 GMT</pubDate></item></channel></rss>