Qt5.6 QML, why are dynamic models destroyed after garbage collection?
<p>I have a variable number of components, so i'm trying to give each one its own <code>model</code>. In this example, i just create one, but the idea is the same.</p>

<p>GC() is a bit random, so in the example, i <em>force</em> the gc() after a click to flush out the problem. What happens is that the <code>model</code> is destroyed and becomes null. after that the click method cannot use it.</p>

<p>main.qml:</p>

<pre><code>import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.2

import com.example.qml 1.0

ApplicationWindow
{
    visible: true
    width: 640
    height: 480

    // builder of dynamic models
    ModelFactory { id: maker }

    Column
    {
        anchors.fill: parent
        Repeater
        {
            // create dynamic model
            model: maker.makeModel();
            delegate: Label
            {
                id: label
                text: model.name

                MouseArea
                {
                    anchors.fill: parent
                    onClicked:
                    {
                        // works once until gc()
                        console.log("clicked on " + model.name)

                        // wont work anymore. model is destroyed
                        gc();
                    }
                }
            }

        }
    }
}
</code></pre>

<p>c++/mymodel.h:</p>

<pre><code>#include &lt;QAbstractListModel&gt;
#include &lt;QQmlApplicationEngine&gt;
#include &lt;QObject&gt;
#include &lt;QString&gt;
#include &lt;QDebug&gt;

class BoxModel : public QAbstractListModel
{
    Q_OBJECT

public:

    ~BoxModel()
    {
        // see that it does get destroyed
        qDebug() &lt;&lt; "~BoxModel()";
    }

    int rowCount(const QModelIndex&amp; parent = QModelIndex()) const override
    {
        return 5;
    }  

    QVariant data(const QModelIndex &amp;index, int role) const override
    {
        int ix = index.row();
        if (ix &lt; 1) return "Larry";
        if (ix &lt; 2) return "Barry";
        if (ix &lt; 3) return "Gary";
        if (ix &lt; 4) return "Harry";
        return "Sally";
    }

    QHash&lt;int, QByteArray&gt; roleNames() const override
    {
        QHash&lt;int, QByteArray&gt; roles;
        roles[Qt::UserRole+1] = "name";
        return roles;
    }

};

class ModelFactory: public QObject
{
    Q_OBJECT

public:

    Q_INVOKABLE BoxModel* makeModel()
    {
        return new BoxModel();
    }    
};
</code></pre>

<p>main.cpp just registers the types:</p>

<pre><code>#include &lt;QGuiApplication&gt;
#include &lt;QQmlApplicationEngine&gt;
#include &lt;qqmlcontext.h&gt;
#include &lt;qqml.h&gt;
#include &lt;QtQuick/qquickitem.h&gt;
#include &lt;QtQuick/qquickview.h&gt;
#include "mymodel.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;

    qmlRegisterType&lt;BoxModel&gt;("com.example.qml", 1, 0, "BoxModel");
    qmlRegisterType&lt;ModelFactory&gt;("com.example.qml", 1, 0, "ModelFactory");

    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    return app.exec();
}
</code></pre>

<p>what you see:</p>

<p><a href="https://i.stack.imgur.com/Jlmx3.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Jlmx3.jpg" alt="runtime"></a></p>

<p>Click on any of the names. it will work once and after that they will be undefined because <code>model</code> becomes null.</p>

<p>eg</p>

<pre><code>qml: clicked on Sally
~BoxModel()
qml: clicked on undefined
</code></pre>

<p>My question is <em>why</em> is this, when i still have a reference to it?</p>

<p>In the example, <code>onClicked</code> could be changed to <code>label.text</code> rather than <code>model.name</code> to fix, but the <em>real</em> problem is that, in general, the <code>model</code> is accessed by the object at any time, for any data. For example, when the box needs to redraw. randomly the data is gone, depending on GC.</p>

<p>I've tried making c++ manage the life of the dynamic model. this could work if i know <em>when</em> exactly QML has finished with it.</p>

<p>thanks for info and ideas.</p>

<p>running on windows 8.1/qt5.6mingw</p>

<p>EDIT1: files as a gist,
<a href="https://gist.github.com/anonymous/86118b67ec804e6149423c14792f312d" rel="nofollow noreferrer">https://gist.github.com/anonymous/86118b67ec804e6149423c14792f312d</a></p>