Important: Please read the Qt Code of Conduct -

Qml/C++ Memory Leak?

  • Hi, i have the next problem, I use Qt 5.5, and create a application with C++ and Qml, but the memory in a Gui where one model are updated every second grows and grows, how i can stop the grow or manage the memory.

    The application works fine, but the problem is in the Gui(page) where a set of models are updated and in the gui are refreshd every second (make a Post and with the answer update the model), in that case after some minutes the memory begins to grow(without limit apparently), i change the model to C++(original are in qml, changed to QQmlListProperty class), but i don't get change in memory usage, i try to use dynamic objects in every method of the process of update with same result, and finally i try to disconnect some notifications of the objects (only the necessaries) from C++ to Qml, but too fails.

    Here the header of C++ QQmlListProperty (QmlListBombs)

    class QmlListBombs:public QObject{
        Q_PROPERTY(QQmlListProperty<BombModel> bombs READ bombs CONSTANT) 
        Q_CLASSINFO("DefaultProperty", "bombs")
           QmlListBombs(QObject *parent=0);
           QQmlListProperty<BombModel> bombs();
          static void append_bomb(QQmlListProperty<BombModel> *list, BombModel *bmb); 
         static BombModel *bombAt(QQmlListProperty<BombModel> *property, const int index);
              QList<BombModel*> m_bombs;
             void bombsChanged();

    The number of bombs when the app is running, always is the same (for that the property is constant)

    Here the method bombs and constructor more

    QmlListBombs::QmlListBombs(QObject *parent):QObject(parent)
    QQmlListProperty<BombModel> QmlListBombs::bombs() {
        return QQmlListProperty<BombModel>this,&m_bombs,&QmlListBombs::bombsSize, &QmlListBombs::bombAt);
    //append function, i not use this funcion to append to list in C++
    void QmlListBombs::append_bomb(QQmlListProperty<BombModel> *list, BombModel *bmb){
    QmlListBombs *bmbsList=qobject_cast<QmlListBombs *>(list->object);
     void QmlListBombs::insertBomb(){
      BombModel *newBomb=new BombModel(island);
    BombModel* QmlListBombs::bombAt(QQmlListProperty<BombModel> *list, const int index){
    return static_cast< QList<BombModel *> *>(list->data)->at(index);
    int QmlListBombs::bombsSize(QQmlListProperty<BombModel> *list){
    return static_cast< QList<BombModel *> *>(list->data)->size();
    //update bomb method, the qjsonobject is from the answer from remote server request
    void QmlListBombs::updateBombs(const QJsonObject &reqStatusPumps){
      int index=0;
       QJsonObject arrayDataStatusReq=reqStatusPumps.value("pumpsList").toObject();
           if(>getStatusBomb()!=arrayDataStatusReq.value(>getPumpNumber() ).toInt())
 >updateStatusPump( arrayDataStatusReq.value(>getPumpNumber() ).toInt() );

    The header of BombModel and part of methods:

    class BombModel: public QObject{
        Q_PROPERTY(QString numberBomb READ getPumpNumber CONSTANT) 
        Q_PROPERTY(int status READ getStatusBomb NOTIFY statusChange)
        Q_PROPERTY(QString statusName READ getStatusName NOTIFY statusNameChange);
          void updateStatusPump(const quint16 &_statusPump){
                         changeStatusName();//this function are only a switch where the status are evaluated
                         emit statusChange();
                         emit statusNameChange();
          QString numberBomb;
           int status;          
          QString statusName;
        void statusChange();
        void statusNameChange();

    From the previous model only have some signals because some data (like pumpNumber, never changes, only change status and statusName), and the list of Bombs never changes too.

    And in the main object i declare the list of QQmlListProperty like a context property.

        view->rootContext();->setContextProperty(QString("listBombs"), listBombs);

    The function who get the actual status is:

        QByteArray responseStatus=catchStatus( jsonStatusBombs );

    Like i say before i don't have problem with visualization, update my problem is the usage of memory.

    Here my Qml file:

                  width: parent.width; height: parent.height;
                  cellWidth: 174; cellHeight: 117;
                  id: arrayBombs;
                  model: listBombs.bombs;
                  delegate: BombModel{
                       numberBomb: model.numberBomb;
                       statusBomb: model.status;
                       statusText: model.statusName;

  • Lifetime Qt Champion


    Not a direct answer but are you deleting unused objects ?

  • @SGaist Yes, and for that is rare the rises of memory, in my actual code (the code posted) i not use more dynamic objects.

  • @SGaist In the past you have a similar case (update objects in real time and show in Qml)? i find alternatives.

  • Lifetime Qt Champion

    What do you mean by realtime ?

  • @SGaist ok, i make a post to server, and with the answer i update the model, the process is repeated every 1/2 second.

  • Lifetime Qt Champion

    In that case, should you rather be using a "real" model based on QAbstractItemModel ?

  • @SGaist ok, is one of my last options (the other is make the post petitions and processing in other thread).