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. [SOLVED] memory leak QListView
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] memory leak QListView

Scheduled Pinned Locked Moved General and Desktop
memory leak
8 Posts 2 Posters 3.1k Views 2 Watching
  • 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.
  • gian71G Offline
    gian71G Offline
    gian71
    wrote on last edited by gian71
    #1

    Hi, I know this is an argument discussed many times but it is not clear to me how to fix my issue.
    In my application I have two models and a QListView.

    the models are updated with this function:
    void TreeModel::setupModelData(bool exp, bool isPresent, int row, const QStringList &lines,
    const QStringList &lines_s, TreeItem *parent){

    (void) parent;
    TreeItem *item = new TreeItem(lines,rootItem);
    if(isPresent){
        rootItem->replaceChild(row,item);
    }else{
        if( row <= 4000){
            rootItem->insertChild(row,item);
        }
        else{
            rootItem->insertChild(row,item);
            rootItem->removeChild(row-4000);
        }
    }
    
    for (int i = 0; i < lines_s.size(); i++){
        QStringList line;
        line =lines_s.at(i).split('\t');
    
        QStringList childrenItems;
        childrenItems << line.at(0) << line.at(1) << line.at(2);
        TreeItem *item2 = new TreeItem(childrenItems,item);
        item->appendChild(item2);
    
    }
    emit sendExtStat(exp,row);
    

    }

    so I create an item and one of the two models has cildren (item2).

    I saw there is big leakage while the models are updating...and I can understand the reason...but I can't figure which options I have to fix it...

    I have tried to reduce the number of items in the model removing items cyclically after 4000...no result.

    Any help on this is very appreciated.
    Here below the main functions used:
    void TreeItem::appendChild(TreeItem *item)
    {
    m_childItems << item;
    }

    void TreeItem::insertChild(int row,TreeItem *item)
    {
    m_childItems.insert(row,item);
    }

    void TreeItem::replaceChild(int row,TreeItem *item)
    {
    m_childItems.replace(row,item);
    }

    void TreeItem::removeChild(int row)
    {
    m_childItems.removeAt(row);
    }

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Since you understand the leak, can you explain what it is ?

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

      1 Reply Last reply
      0
      • gian71G Offline
        gian71G Offline
        gian71
        wrote on last edited by
        #3

        Well my understanding of the leak is that I create new items (memory allocation) and I never free their memory after I add the items to the tree and even when I remove the children from the tree. If my understanding of the leak is not correct, then I would appreciate if somebody can explain or give me some link where I can learn more.

        Thanks

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Since you are writing your own model, it's your role to delete the items that are not used anymore

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

          1 Reply Last reply
          0
          • gian71G Offline
            gian71G Offline
            gian71
            wrote on last edited by
            #5

            The memory leak is not longer there. I delete the Items when they are not longer needed. So basically after 4000 items, every time I append a new item I remove/delete the first one in the tree.

            this is the code:
            if( row <= 4000){
            rootItem->insertChild(row,item);
            }
            else{
            rootItem->insertChild(4001,item);
            rootItem->removeFirst();
            }

            void TreeItem::removeFirst()
            {
            delete m_childItems.takeFirst();
            }

            There was a second leak for a second model where instead to continuously append Items I overwrite those which are already present in the tree. This leak is also ok now , I do not allocate memory for items already present in the tree, I just update their data.

                if (collaps && isPresent){
                    item2 = item->child(i);
                    item2->setm_childData(childrenItems); // data update
                } else{
                    item2 = new TreeItem(childrenItems,item); // item not already present 
                    item->appendChild(item2);
                }
            

            Everything works fine, I don't have leak anymore but for this second model I can't see the view update. I am sure the data are updated because if I reset the model the values are displayed once.

            I am looking the simple tree model, the function creatIndex, beginInsertRows, changeData()...but nothing is working to update the view. How can I do that?

            Thanks

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              IIRC, there was a good example in the "C++ GUI Programming with Qt4" book, you should be able to get the examples on line (note that the book is worth reading even if it's Qt 4, it still applies to Qt 5)

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

              1 Reply Last reply
              0
              • gian71G Offline
                gian71G Offline
                gian71
                wrote on last edited by
                #7

                update...
                I have found a solution. Thanks again for the example...it is actually good but i have also spent some more time on other examples (like this...http://doc.qt.io/qt-5/qt widgets-itemviews-editable treemodel-example.html).

                The most difficult part is to understand how to use indexes to update the model ( bit confusing but eventually I got there, with some pain).

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Indeed, that part can be a bit tricky at beginning :)

                  Glad you found out !

                  Happy coding !

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

                  1 Reply Last reply
                  0

                  • Login

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