Cleaning up QVector/QContainers?
-
Hey
When I delete obejct that had QVector<item>/QVector<QMap<item,int>> etc etc assuming the items are not *, do I need to do anything to vectors in ~delete of object holding them or are they automatically cleared/memory released? I'm trying to understand my memory leak :- )
-
-
When a container holds objects, the object is deleted when the container is cleared (not Qt-specific)
-
Adding to @Christian-Ehrlicher the object in the container shall have a proper destructor not creating memory leaks itself.
-
So I though, but as far as I remember... .clear() does not release memory. I have to call something else afterwards to actually release memory. Which make me wonder... if I have to call it in destructor as well ?
Say
class ob{ QVector<int>ints; QVector<otherOB> otherObs ~ob(){ ints.clear(); ints.squeeze(); otherObs.clear(); otherObs.squeeze(); }
?
-
@Dariusz said in Cleaning up QVector/QContainers?:
indestructor as wel
No, why should you? The container is destroyed automatically in the dtor - c++ basics.
-
@Christian-Ehrlicher Ok so I don't have to do the ~ob(){} example above in objects at all as all will automatically clear up ?
-
-
@Dariusz said in Cleaning up QVector/QContainers?:
So I though, but as far as I remember... .clear() does not release memory. I have to call something else afterwards to actually release memory. Which make me wonder... if I have to call it in destructor as well ?
clear()
releases the memory used by theQVector
. If you put pointers in there, then it's going to free the memory the pointers occupied, not the memory the pointers reference - that'd be your job.
In all waysQVector<>
is behaving like any other RAII-compliant type.Calling
clear()
is not necessary, nor is callingsqueeze()
, the vector does that automatically when it destructs. Here's a relevant paragraph: http://eel.is/c++draft/stmt.dcl#2 -
Hi,
To delete the content there's qDeleteAll.