Cant free memory used by QList



  • Hi,

    I am using two QLists in my dataobject, one with QString and the other with int.
    If I make some instances of my dataobject, delete them and create them again, the
    memory is increasing and so on.
    I tried delete, qDeleteAll, clear but nothing worked.
    Is there a way or should I use an array?



  • Maybe you could provide us with some code snippets? It's hard to tell why your memory is leaking - maybe you are using pointers to memory areas that are not deleted?



  • ok.

    thats my dataobject
    @
    class dataobj {
    QList<QString> Names;
    QList<int> Values;
    }@

    I use the code that way:
    @
    QList<dataobj*>tmp;
    for(int i = 0; i<MAX_DATA;i++){
    tmp.append(new dataobj());
    tmp.at(i)->Names.append("test1");
    tmp.at(i)->Names.append("test2");
    tmp.at(i)->Values.append(2);
    tmp.at(i)->Values.append(5);
    }@

    in the destructor I delete every instance of the dataobject:
    @
    for(int i = 0; i<MAX_DATA;i++){
    delete tmp.at(i);
    }
    tmp.clear();
    @



  • Have a look at "this thread":https://qt-project.org/forums/viewthread/13816/. I think the solutions sugested there can be applied to your case as well.



  • How do you measure memory usage?



  • I did this with "top".
    But yesterday I have rebuilt my builtroot and now
    its working. Its really strange because the settings are the same.



  • top is not showing the memory presently used by a program: it shows the maximum amount of memory used by a program (or process). Part of the "used" memory may be freed but still "assigned" to the program in case it needs some more. The memory seen with top won't decrease unless some other program needs some more and cannot find any free memory.

    I've also been tricked by this behaviour ;-)



  • Indeed, top isn't suitable for this. I would suggest using valgrind or something similar to check wether you actually do have a memory leak. The wiki page "Tools for Profiling and Memory Checking":/wiki/Profiling-and-Memory-Checking-Tools contains a list of suitable tools for various platforms.


Log in to reply
 

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