Leak memory when call setContextProperty to update list object has changed



  • I use setContextProperty to send data from C++ to QML. Every times the data changes, I call setContextProperty again to update the data to QML for displaying.
    But the memory increase each time I call setContextProperty without descrease. I found that there is only one way to update data with setContextProperty.Please refer..

    Note: There is no way for the view to know that the contents of a QStringList have changed. If the QStringList changes, it will be necessary to reset the model by calling QQmlContext::setContextProperty() again.
    

    I think the problem related to the old data of las setContextProperty not free before update new data. How can I resolve this problem. Please help.
    I found my problem is similar with this post:https://forum.qt.io/topic/7729/memory-leak-or-misunderstanding-with-qlist-qobject-as-model


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Not a direct answer but since your QStringList changes over time, why not use a QStringListModel. That way when there's a change it will be communicated automatically.

    Hope it helps



  • currently, I use QList<QObject*> as model of listview. This listview contain some repeater of object model (composite design pattern). Could you please show me the way change to QListModel or something like that?
    Also, the direct solution for this problem is prefer (because my source become big now)


  • Lifetime Qt Champion

    Did you already saw the Using C++ Models with Qt Quick Views chapter of Qt's documentation ?



  • Currently, I use QQmlListProperty to export data from C++ to QML. My data has structure like below:
    grandgrandparent has list of grandparent: QList<grandparent*> in grandgrandparent class
    grandparent has list of parent: QList<parent*> in grandparent class
    parent has list of child: QList<child*> in parent class

    Currently, when I update data in grandgrandparent (QList<grandparent*>) by call setContextProperty, all data of children update automatically. The problem here is I must update data by calling setContextProperty many times and cause leak memory. Is there any way to keep this design and update changed data without leak memory?


  • Lifetime Qt Champion

    Shouldn't you rather have a tree model to represent that ?



  • @SGaist But the grandparent, parent and child are different object.
    0_1472720446562_Untitled.png
    My model have structure and display like above picture(I have three class represents 3 object views) My model contains list grandparent object. grand parent contains lst parent object and parent object contain list child object.
    Currently, I use Listview to display list grandprarent, repeater of column to display parent view, and repeater of row to display child view.
    Current I use QQmlListProperty to store list child, list parent. QList<grandparent> is QList<QObject*> and export to QML by setContextProperty().
    With this structure data, I can modify data of child object easy and can get it from QML code.
    Could you please recommend me the proper model for this structure?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.