Qt Quick Windows app crashes when C++ try to access element (in range) of a vector
<p>My Qt Quick application on Windows is experiencing a very weird crash. </p>

<p>I have a <code>QObject</code> derived c++ class called <code>QObjectVector</code> that allows QML side to access QObject pointers in a container.
On the QML side, I have a bunch of buttons displaying names of objects in that container. when I scroll the mouse wheel, I manually update each button's <code>currentItem</code> property.</p>

<p>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>QObjectVector.h</p>

<pre><code>#pragma once

#include &lt;QObject&gt;
#include &lt;QVector&gt;

#include "Types.h"

namespace LPP
{
    class QObjectVector : public QObject
    {
        Q_OBJECT

        Q_PROPERTY(Int size READ size NOTIFY sizeChanged)

    public:
        explicit QObjectVector();
        virtual ~QObjectVector();

        Int size();
        Q_INVOKABLE QObject* at(Int);
        void push(QObject*);
        void remove(Int);
        void remove(QObject*);

        QVector&lt;QObject*&gt; &amp;getData();

        bool deleteChildrenOnDestroy;

    private:
        QVector&lt;QObject*&gt; m_data;

    signals:
        void sizeChanged();

    public slots:
    };
}
</code></pre>

<p>QObjectVector.cpp</p>

<pre><code>...
    QObject* QObjectVector::at(Int i)
    {
        qDebug() &lt;&lt; "size: " &lt;&lt; this-&gt;m_data.size();
        for (int i = 0; i &lt; this-&gt;m_data.size(); i++){
            qDebug() &lt;&lt; "i: " &lt;&lt; i &lt;&lt; " item: ";
            //when I scroll rapidly, crash happens here when I trace objects in vector
            qDebug() &lt;&lt; this-&gt;m_data[i];
        }
        qDebug() &lt;&lt; "returning.";
        return (i &gt;= 0 &amp;&amp; i &lt; this-&gt;m_data.size()) ? this-&gt;m_data[i] : nullptr;
    }
....
</code></pre>

<p>ButtonContainer.qml</p>

<pre><code>....
    //this function is called when I scroll mouse wheel.
    //itemList contains list of Buttons

    function refreshDisplay() {
        var i, j = 0;

        for (i = 0; i &lt; itemList.length; i++){
            itemList[i].visible = itemList[i].enabled = false;
        }

        var start = Utils.clamp(Math.floor(scrollView.flickableItem.contentY / (itemHeight + itemVSpacing)), 0, currentFolder.size);
        var end = Utils.clamp(Math.ceil((scrollView.flickableItem.contentY + scrollView.flickableItem.height) / (itemHeight + itemVSpacing)), 0, currentFolder.size);

        var item;

        for (i = start; i &lt; end; i++){
            if (j &gt;= itemList.length){
                itemList.push(tableItem_comp.createObject(tableArea));
            }

            itemList[j].visible = itemList[j].enabled = true;

            item = currentFolder.at(i);
            itemList[j].currentItem = item;
            j++;
        }
    }
....
</code></pre>

<p>Button.qml</p>

<pre><code>....
    property var currentItem: null;

    anchors.left: parent.left
    anchors.right: parent.right

    SimpleButton {
        id: button
        width: 100
        height: 50
        text: currentItem.name;
        onClicked: {
            viewer.select(currentItem);
        }
    }
....
</code></pre>

<p>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>Thank you!</p>

<p>Tommy</p>