[SOLVED] memory leak QListView



  • 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);
    }


  • Lifetime Qt Champion

    Hi,

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



  • 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


  • Lifetime Qt Champion

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



  • 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


  • Lifetime Qt Champion

    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)



  • 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).


  • Lifetime Qt Champion

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

    Glad you found out !

    Happy coding !


Log in to reply
 

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