PDA

View Full Version : Dynamic memory release



linuxdev
10th December 2008, 14:58
Hi,

I am facing memory leak, issues.

1. I have quite a few QVector's in my app, to store data, does a call to QVector::clear(), release all the memory that is occupied by it?.

2. I have a pointer to QVector(Size of which grows enormously), when i am done with my job, i do delete on the pointer.
Does this release all the memory or do i have to clear the QVector before doing delete on the pointer to have successfull memory release?

3.I have a QHash<int, pointer>, to draw some data on the display.
i have used QHash::values(), to get the data to be drawn.
When i am done with one key, i call QHash::remove(key).
After remove is done, will QHash::values() return the value pointed to by 'key'?
Is it safe to use QHash::values() directly without getting list of keys and then there values....?

I have already done a delete on pointer, but my app, still shows an increase in VIRTUAL MEMORY(output from pmap) continuosly after delete is done ...what can be the issue?

Looking forward to your response...

caduel
10th December 2008, 15:04
It releases the memory it allocated itself.
If you store pointers in the QVector, and those should happen (because they don't need to) to have been allocated on the heap (with new)... then this allocated memory will not be freed.


One simple solution is to put smart pointers (a boost::shared_ptr, or Qt 4.5's upcoming smart ptrs) inside such containers.
Another way would be to call qDeleteAll before clearing the container.

HTH

linuxdev
10th December 2008, 15:09
Hi ,

I am not storing pointer's in QVector...
I have a QHash<int, pointer>
The pointer in Hash points to a QVector, before i remove a key from hash, i have called delete on the pointer (to ensure that all the memory alloted to QVector its pointing is released).
Is this enough or should i have to clear the QVector that the pointer points to ?.

caduel
10th December 2008, 15:46
With respect to memory leaks: deleting that pointer is enough.
The memory allocated by the QHash itself is managed automatically. You just have to free the memory you allocated.

linuxdev
11th December 2008, 05:21
I have already done that (I have done a delete on the pointer in the Hash before removing the key), but my app shows a constant increase in the VIRTUAL MEMORY (using pmap), only while handling the data related to QHash<int, pointer>.

I suspect only the pointer in QHash, pointing to QVector ....

I am running 'top' command on the exe, at regular intervals and its showing a constant increase in Virtual Memory and resident memory. (Specifically in the anon Block, i think its Heap and related to Dynamic Memory i am using)

Is there any way i can find out, why this is happening ? or which data in my app is taking memory and not releasing back to the Heap???....

Is there any better way to detect Memory Leak in Qt.
I have used Valgrind, but its at very low level...

spirit
11th December 2008, 06:22
put qDebug in all dtors of your object and count number of messages after deleting or put breakpoints in all dtors and trace them or you can use 'valgrind' under Linux.