<?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[Unavailable synchronous data using QSharedData]]></title><description><![CDATA[<p dir="auto">Hello,<br />
I wrote my first Implicit Sharing Class. Something goes wrong, I do not know why.</p>
<p dir="auto">Here is my implementation:<br />
@<br />
<a href="//mymap.h" target="_blank" rel="noopener noreferrer nofollow ugc">//mymap.h</a><br />
#ifndef MYMAP_H<br />
#define MYMAP_H</p>
<p dir="auto">#include &lt;QSharedDataPointer&gt;<br />
#include &lt;QVector&gt;</p>
<p dir="auto">namespace kernel_interface {<br />
class MyMapData;</p>
<p dir="auto">class MyMap<br />
{<br />
public:<br />
MyMap();<br />
MyMap(const MyMap &amp;);<br />
MyMap &amp;operator=(const MyMap &amp;);<br />
~MyMap();<br />
QVector&lt;const float *&gt; getValue(int type) const;<br />
void appendValue(int type, QVector&lt; const float *&gt; value);<br />
void appendValue(int type, const float * value);</p>
<p dir="auto">private:<br />
QSharedDataPointer&lt;MyMapData&gt; _data;<br />
};</p>
<p dir="auto">} /* end namspace kernel_interface */<br />
#endif // MYMAP_H<br />
@</p>
<p dir="auto">@<br />
<a href="//mymap.cpp" target="_blank" rel="noopener noreferrer nofollow ugc">//mymap.cpp</a><br />
namespace kernel_interface {</p>
<p dir="auto">MyMap::MyMap() : _data(new MyMapData)<br />
{<br />
}</p>
<p dir="auto">MyMap::MyMap(const MyMap &amp;rhs) : _data(rhs._data)<br />
{<br />
}</p>
<p dir="auto">MyMap &amp;MyMap::operator=(const MyMap &amp;rhs)<br />
{<br />
if (this != &amp;rhs)<br />
_data.operator=(rhs._data);<br />
return *this;<br />
}</p>
<p dir="auto">MyMap::~MyMap() {}</p>
<p dir="auto">void MyMap::appendValue(int type, QVector&lt;const float * &gt; value)<br />
{<br />
QVector&lt;const float * &gt; tmp = _data-&gt;_map.value(type);<br />
tmp += value;<br />
_data-&gt;_map.insert(type, tmp);<br />
}</p>
<p dir="auto">void MyMap::appendValue(int type, const float * value)<br />
{<br />
QVector&lt;const float * &gt; tmp = _data-&gt;_map.value(type);<br />
tmp.append(value);<br />
_data-&gt;_map.insert(type, tmp);<br />
}</p>
<p dir="auto">QVector&lt;const float * &gt; MyMap::getValue(int type) const<br />
{<br />
return _data-&gt;_map.value(type);<br />
}</p>
<p dir="auto">}/* end namespace kernel_interface */<br />
@<br />
@<br />
<a href="//mymapdata.h" target="_blank" rel="noopener noreferrer nofollow ugc">//mymapdata.h</a><br />
#ifndef MYMAPDATA_H<br />
#define MYMAPDATA_H</p>
<p dir="auto">#include &lt;QSharedData&gt;<br />
#include &lt;QMap&gt;<br />
#include &lt;QVector&gt;</p>
<p dir="auto">namespace kernel_interface {</p>
<p dir="auto">class MyMapData : public QSharedData<br />
{<br />
public:<br />
MyMapData() {}<br />
~MyMapData() {<br />
/*   QMapIterator&lt;int, QVector&lt; const float *&gt; &gt; i(_map);<br />
while(i.hasNext())<br />
{<br />
QVector&lt; const float *&gt; tmp = (i.next()).value();<br />
QVectorIterator&lt; const float <em>&gt; j(tmp);<br />
while(j.hasNext())<br />
{<br />
delete j.next();<br />
}<br />
}</em>/<br />
qDebug() &lt;&lt; "kaputt";<br />
}<br />
QMap&lt;int, QVector&lt; const float *&gt; &gt; _map;<br />
};</p>
<p dir="auto">#endif // MYMAPDATA_H</p>
<p dir="auto">} /* end namespace kernel_interface */</p>
<p dir="auto">@</p>
<p dir="auto">So now what happens:<br />
@<br />
<a href="//main.cpp" target="_blank" rel="noopener noreferrer nofollow ugc">//main.cpp</a><br />
kernel_interface::MyMap func()<br />
{<br />
float *insert = new float[3];<br />
insert[0] = 1.5;<br />
insert[1] = 2.0;<br />
insert[2] = 3.0;</p>
<pre><code>kernel_interface::MyMap ret;
ret.appendValue(0, insert);
return ret;
</code></pre>
<p dir="auto">}</p>
<p dir="auto">main() {<br />
a = func();  // <em>step 1</em><br />
b.appendValue(0, a.getValue(0));  //* step 2*<br />
a = func();  // <em>step 3</em><br />
b.appendValue(0, a.getValue(0));  // <em>step 4</em><br />
@</p>
<p dir="auto">The output after <em>step 1</em><br />
<em>a</em>  kernel_interface::MyMap<br />
_data  QSharedDataPointer&lt;kernel_interface::MyMapData&gt;<br />
QSharedData ref: 1 QSharedData<br />
_map &lt;1 items&gt; QMap&lt;int, QVector&lt;float const*&gt;&gt;<br />
[0]   QMapNode&lt;int, QVector&lt;float const*&gt;&gt;<br />
key 0 (0) int<br />
value &lt;1 items&gt; QVector&lt;float const*&gt;<br />
[0] 1.5 @0xb0c4c0 float</p>
<p dir="auto"><em>b</em>  kernel_interface::MyMap<br />
_data  QSharedDataPointer&lt;kernel_interface::MyMapData&gt;<br />
QSharedData ref: 1 QSharedData<br />
_map &lt;0 items&gt; QMap&lt;int, QVector&lt;float const*&gt;&gt;</p>
<p dir="auto">The output after <em>step 2</em><br />
<em>a</em>  kernel_interface::MyMap<br />
_data  QSharedDataPointer&lt;kernel_interface::MyMapData&gt;<br />
QSharedData ref: 1 QSharedData<br />
_map &lt;1 items&gt; QMap&lt;int, QVector&lt;float const*&gt;&gt;<br />
[0]   QMapNode&lt;int, QVector&lt;float const*&gt;&gt;<br />
key 0 (0) int<br />
value &lt;1 items&gt; QVector&lt;float const*&gt;<br />
[0] 1.5 @0xb0c4c0 float</p>
<p dir="auto"><em>b</em> kernel_interface::MyMap<br />
_data  QSharedDataPointer&lt;kernel_interface::MyMapData&gt;<br />
QSharedData ref: 1 QSharedData<br />
_map &lt;1 items&gt; QMap&lt;int_type, QVector&lt;float const*&gt;&gt;<br />
[0]   QMapNode&lt;int, QVector&lt;float const*&gt;&gt;<br />
key 0 (0) int<br />
value &lt;1 items&gt; QVector&lt;float const*&gt;<br />
[0] <em>1.5 @0xb0c4c0</em> float</p>
<p dir="auto">The output after <em>step 3</em><br />
<em>a</em>  kernel_interface::MyMap<br />
_data  QSharedDataPointer&lt;kernel_interface::MyMapData&gt;<br />
QSharedData ref: 1 QSharedData<br />
_map &lt;1 items&gt; QMap&lt;int, QVector&lt;float const*&gt;&gt;<br />
[0]   QMapNode&lt;int, QVector&lt;float const*&gt;&gt;<br />
key 0 (0) int<br />
value &lt;1 items&gt; QVector&lt;float const*&gt;<br />
[0] 1.5 @0xb0f650 float</p>
<p dir="auto"><em>b</em>  kernel_interface::MyMap<br />
_data  QSharedDataPointer&lt;kernel_interface::MyMapData&gt;<br />
QSharedData ref: 1 QSharedData<br />
_map &lt;1 items&gt; QMap&lt;int, QVector&lt;float const*&gt;&gt;<br />
[0]   QMapNode&lt;int, QVector&lt;float const*&gt;&gt;<br />
key 0 (0) int<br />
value &lt;1 items&gt; QVector&lt;float const*&gt;<br />
[0] <em>0 @0xb0c4c0</em> float</p>
<p dir="auto">The output after <em>step 4</em><br />
<em>a</em>  kernel_interface::MyMap<br />
_data  QSharedDataPointer&lt;kernel_interface::MyMapData&gt;<br />
QSharedData ref: 1 QSharedData<br />
_map &lt;1 items&gt; QMap&lt;int, QVector&lt;float const*&gt;&gt;<br />
[0]   QMapNode&lt;int, QVector&lt;float const*&gt;&gt;<br />
key 0 (0) int<br />
value &lt;1 items&gt; QVector&lt;float const*&gt;<br />
[0] 1.5 @0xb0f650 float</p>
<p dir="auto"><em>b</em>  kernel_interface::MyMap<br />
_data  QSharedDataPointer&lt;kernel_interface::MyMapData&gt;<br />
QSharedData ref: 1 QSharedData<br />
_map &lt;1 items&gt; QMap&lt;int, QVector&lt;float const*&gt;&gt;<br />
[0]   QMapNode&lt;int, QVector&lt;float const*&gt;&gt;<br />
key 0 (0) int<br />
value &lt;2 items&gt; QVector&lt;float const*&gt;<br />
[0] <em>0 @0xb0c4c0</em> float<br />
[1] 1.5 @0xb0f650 float</p>
<p dir="auto">So why do the values in <em>b</em> get invalid when the =operator is applied on <em>a</em>? If I take bigger float areas the debugger says:unavailable synchronous data</p>
<p dir="auto">Help is very much appreciated! kodiak</p>
]]></description><link>https://forum.qt.io/topic/11719/unavailable-synchronous-data-using-qshareddata</link><generator>RSS for Node</generator><lastBuildDate>Mon, 08 Jun 2026 10:47:29 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/11719.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 28 Nov 2011 11:44:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Unavailable synchronous data using QSharedData on Mon, 28 Nov 2011 20:29:33 GMT]]></title><description><![CDATA[<p dir="auto">I am a step further. I figured out that the allocated memory by the float gets invalid for some reason. I do not know enough about it to say why. Maybe somebody can tell me, so I do understand more what I am doing.</p>
<p dir="auto">What helped at least with =operator was deep copying the floats</p>
<p dir="auto">@<br />
MyMap &amp;MyMap::operator=(const MyMap &amp;rhs) {<br />
if (this != &amp;rhs) {<br />
_data-&gt;_map.clear();<br />
QMapIterator&lt;int, QVector&lt; const float *&gt; &gt; i(rhs._data-&gt;_map);<br />
while(i.hasNext())<br />
{<br />
QVector&lt; const float *&gt; tmp = (i.next()).value();<br />
QVector&lt; const float *&gt; tmp2;<br />
QVectorIterator&lt; const float *&gt; j(tmp);<br />
while(j.hasNext())<br />
{<br />
const float *p = j.next();<br />
float *tmp3 = new float[3];<br />
if(p != 0) {<br />
mempcpy(tmp3,p, 3);<br />
delete []p;<br />
}<br />
tmp2.append(tmp3);<br />
}<br />
_data-&gt;_map.insert(i.key(),tmp2);<br />
}<br />
}<br />
}<br />
@</p>
<p dir="auto">I do not know if I did it right, some feedback would be great!</p>
]]></description><link>https://forum.qt.io/post/118518</link><guid isPermaLink="true">https://forum.qt.io/post/118518</guid><dc:creator><![CDATA[kodiak]]></dc:creator><pubDate>Mon, 28 Nov 2011 20:29:33 GMT</pubDate></item></channel></rss>