Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Aug 2006

    Quick Quer regarding memory leak !!


    I am workign on QT application. I have two scenario under which I am not clear if the memory is being freed or I have to do some thing to free the same

    Case 1:

    void myFunction(QNetworkReply* reply)
    QByteArray datum = reply->readAll();

    When I exist the function I assume the memory allocated to QByteArray which is local to function(allocated on Stack) will get freed. or do I have to take some other steps to make sure it gets freed properly. Like calling clear() before leaving the function.

    Case 2:

    I have the created a queue using the

    QList< QPair<QString, QPixmap> > queue;

    queue is member variable of class.

    I have function which keeps on adding the QPixmap to QList. When the count reach to 20 it starts from 0 element of QLIst.

    When I insert the QPixmap on pos say 0 where already there is pixmap, will the previous pix map cleanly freed?
    Also when i I call queue.clear to empty the queue Will it free the memory?

    I know these are simple queries as memory is being allocated to stack. But I need to make sure everything is cleaned up.

    Comments are appreciated.

    PS: Before posting do try wiki search to save your time and effort:

  2. #2
    Super Contributor
    Join Date
    Nov 2009
    Minnesota, USA

    Re: Quick Quer regarding memory leak !!

    Any time you have an object that is referenced as a variable rather than through a pointer (ie, there's no "*" in the declaration), the object lifetime is scoped to the place where the object is declared. If the object is an automatic variable then it is scoped to the method invocation and will be deleted when you exit the method. If the object is declared as a member of some other object, the object will be deleted when the containing object is deleted.

    QList, QPair, QString, and QPixmap are all "assignable" data types -- ie, they implement "operator=" and under the covers are "thin" objects with a reference-counted "fat" object behind them. When they are assigned to an "empty" location the entire "thin" object is copied and the reference count of the "fat" object is incremented. If a new value is assigned to one of these objects, the reference count of the old "fat" object is decremented before the "thin" object is overwritten.

    As a result, if you don't play to many tricks these objects are "airtight" -- no leaks.

    Note, however, that if you use pointers (eg, QPixmap*) then the onus is on you to manage object lifetime.

Similar Threads

  1. Need Help: Memory Leak
    By max_gudluck in forum Symbian
    Replies: 2
    Last Post: 2008-12-21, 07:41
  2. Memory leak
    By raulftang in forum Symbian
    Replies: 24
    Last Post: 2007-03-17, 22:38
  3. memory leak on emulator = memory leak on device
    By manmli in forum Mobile Java General
    Replies: 2
    Last Post: 2006-08-05, 19:54
  4. Initial heap memory allocation - quick Q
    By jhodgski in forum Mobile Java General
    Replies: 0
    Last Post: 2004-05-06, 17:09
  5. quick memory question
    By aed02 in forum Mobile Java General
    Replies: 6
    Last Post: 2003-07-24, 13:45

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts