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_OBJECT Q_PROPERTY(QQmlListProperty<BombModel> bombs READ bombs CONSTANT) Q_CLASSINFO("DefaultProperty", "bombs") public: 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); private: QList<BombModel*> m_bombs; signals: 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
//constructor 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); if(bmbsList){ bmb->setParent(bmbsList); bmbsList->m_bombs.append(bmb); } } void QmlListBombs::insertBomb(){ BombModel *newBomb=new BombModel(island); newBomb->setParent(this); m_bombs.append(newBomb); } 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(); while(index<sizeQmlListBombs()){ if(m_bombs.at(index)->getStatusBomb()!=arrayDataStatusReq.value( m_bombs.at(index)->getPumpNumber() ).toInt()) m_bombs.at(index)->updateStatusPump( arrayDataStatusReq.value( m_bombs.at(index)->getPumpNumber() ).toInt() ); index++ } }
The header of BombModel and part of methods:
class BombModel: public QObject{ Q_OBJECT Q_PROPERTY(QString numberBomb READ getPumpNumber CONSTANT) Q_PROPERTY(int status READ getStatusBomb NOTIFY statusChange) Q_PROPERTY(QString statusName READ getStatusName NOTIFY statusNameChange); public: void updateStatusPump(const quint16 &_statusPump){ if(this->status!=_statusPump){ this->status=_statusPump; changeStatusName();//this function are only a switch where the status are evaluated emit statusChange(); emit statusNameChange(); } } private: QString numberBomb; int status; QString statusName; signals: 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:
GridView{ width: parent.width; height: parent.height; anchors.top: parent.top; cellWidth: 174; cellHeight: 117; id: arrayBombs; model: listBombs.bombs; delegate: BombModel{ numberBomb: model.numberBomb; statusBomb: model.status; statusText: model.statusName; } }
-
Hi,
Not a direct answer but are you deleting unused objects ?
-
What do you mean by realtime ?
-
In that case, should you rather be using a "real" model based on QAbstractItemModel ?