Results 1 to 6 of 6

Thread: Dynamic memory release

  1. #1
    Join Date
    Dec 2008
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Dynamic memory release

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

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dynamic memory release

    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

  3. #3
    Join Date
    Dec 2008
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic memory release

    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 ?.

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dynamic memory release

    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.

  5. #5
    Join Date
    Dec 2008
    Posts
    31
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic memory release

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

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Dynamic memory release

    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.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

Similar Threads

  1. Replies: 0
    Last Post: 26th September 2008, 21:33
  2. Memory management
    By cyberboy in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2008, 20:48
  3. Static vs. Dynamic Building for GPL Open Source Software Release
    By brent99 in forum Installation and Deployment
    Replies: 6
    Last Post: 11th March 2008, 20:46
  4. Memory leak weirdness
    By Darhuuk in forum General Programming
    Replies: 10
    Last Post: 10th January 2008, 18:51
  5. It seems that Qt does not release unused memory
    By iw2nhl in forum Qt Programming
    Replies: 31
    Last Post: 25th November 2007, 20:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.