Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qml/C++ Memory Leak?
QtWS25 Last Chance

Qml/C++ Memory Leak?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qmlc++qt5.5memory leakmemory managmen
8 Posts 2 Posters 4.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Arturo Pablo R
    wrote on 9 Mar 2016, 20:08 last edited by Arturo Pablo R 3 Sept 2016, 20:09
    #1

    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;
                 }
          }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 9 Mar 2016, 21:14 last edited by
      #2

      Hi,

      Not a direct answer but are you deleting unused objects ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 2 Replies Last reply 9 Mar 2016, 23:26
      0
      • S SGaist
        9 Mar 2016, 21:14

        Hi,

        Not a direct answer but are you deleting unused objects ?

        A Offline
        A Offline
        Arturo Pablo R
        wrote on 9 Mar 2016, 23:26 last edited by Arturo Pablo R 3 Sept 2016, 23:29
        #3

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

        1 Reply Last reply
        0
        • S SGaist
          9 Mar 2016, 21:14

          Hi,

          Not a direct answer but are you deleting unused objects ?

          A Offline
          A Offline
          Arturo Pablo R
          wrote on 9 Mar 2016, 23:33 last edited by Arturo Pablo R 3 Oct 2016, 00:21
          #4

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

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 11 Mar 2016, 23:14 last edited by
            #5

            What do you mean by realtime ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            A 1 Reply Last reply 16 Mar 2016, 17:01
            0
            • S SGaist
              11 Mar 2016, 23:14

              What do you mean by realtime ?

              A Offline
              A Offline
              Arturo Pablo R
              wrote on 16 Mar 2016, 17:01 last edited by
              #6

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

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 16 Mar 2016, 21:51 last edited by
                #7

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                A 1 Reply Last reply 18 Mar 2016, 17:31
                0
                • S SGaist
                  16 Mar 2016, 21:51

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

                  A Offline
                  A Offline
                  Arturo Pablo R
                  wrote on 18 Mar 2016, 17:31 last edited by
                  #8

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

                  1 Reply Last reply
                  0

                  5/8

                  11 Mar 2016, 23:14

                  • Login

                  • Login or register to search.
                  5 out of 8
                  • First post
                    5/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved