<?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[Qt Quick Windows app crashes when C++ try to access element (in range) of a vector]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">My Qt Quick application on Windows is experiencing a very weird crash.</p>
<p dir="auto">I have a <code>QObject</code> derived c++ class called <code>QObjectVector</code> that contains a <code>QVector&lt;QObject*&gt; m_data</code> to store some objects created on the heap. I have a <code>Q_INVOKABLE QObject* QObjectVector::at(index)</code> function for QML side to access elements in that vector.<br />
On the QML side, I have a bunch of buttons, each with a property <code>currentObject</code>, and the button text is bound to <code>currentObject.name</code>. when I scroll the mouse wheel, I manually update the <code>currentObject</code> of each button using the <code>at</code> function (the text get updated too by the binding).</p>
<p dir="auto">Strangely, and only <em>sometimes</em>, after an <em>unpredictable</em> number of fast scrolling, the program crashes (stopped working). After debugging, I found that the crashing happens when the <code>at</code> function access <code>m_data[i]</code>. I have made sure that <code>i</code> is within the valid range [0, <code>m_data.size()</code>), so it is not an index-out-of-range error. My guess is that, somehow, the QML engine deletes my objects on the heap when trying to manage memory.</p>
<p dir="auto">This problem is very weird and unpredictable, and solving it is very crucial to the development of my app, so please please please, any help will be appreciated.</p>
<p dir="auto">Thank you!</p>
<p dir="auto">Tommy</p>
]]></description><link>https://forum.qt.io/topic/69634/qt-quick-windows-app-crashes-when-c-try-to-access-element-in-range-of-a-vector</link><generator>RSS for Node</generator><lastBuildDate>Tue, 09 Jun 2026 05:33:43 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/69634.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 26 Jul 2016 14:13:29 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Qt Quick Windows app crashes when C++ try to access element (in range) of a vector on Tue, 26 Jul 2016 15:19:25 GMT]]></title><description><![CDATA[<p dir="auto">@Wieland</p>
<p dir="auto">Thank you so much! I added <code>QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership);</code> when the object was created, and this apparently solves the problem :)</p>
]]></description><link>https://forum.qt.io/post/339377</link><guid isPermaLink="true">https://forum.qt.io/post/339377</guid><dc:creator><![CDATA[TommyX]]></dc:creator><pubDate>Tue, 26 Jul 2016 15:19:25 GMT</pubDate></item><item><title><![CDATA[Reply to Qt Quick Windows app crashes when C++ try to access element (in range) of a vector on Tue, 26 Jul 2016 14:28:22 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/tommyx">@<bdi>TommyX</bdi></a> said:</p>
<blockquote>
<p dir="auto">My guess is that, somehow, the QML engine deletes my objects on the heap when trying to manage memory.</p>
</blockquote>
<p dir="auto">You're right. When you return a <code>QObject*</code> from C++ to QML, the default behaviour is that the QML engine takes ownership of the object. You can change this, see: <a href="http://doc.qt.io/qt-5/qtqml-cppintegration-data.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.io/qt-5/qtqml-cppintegration-data.html</a> and <a href="http://doc.qt.io/qt-5/qqmlengine.html#setObjectOwnership" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.io/qt-5/qqmlengine.html#setObjectOwnership</a>.</p>
]]></description><link>https://forum.qt.io/post/339364</link><guid isPermaLink="true">https://forum.qt.io/post/339364</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Tue, 26 Jul 2016 14:28:22 GMT</pubDate></item></channel></rss>