Results 1 to 4 of 4

Thread: QList<pointer>::takeFirst() and memory leak question

  1. #1
    Join Date
    Jun 2008
    Location
    Germany/Belarus/Sweden
    Posts
    53
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QList<pointer>::takeFirst() and memory leak question

    Hi All!
    Can't think clearly today. So, just to be assured, has decided to ask:
    I have QList created somewhere in program:
    Qt Code:
    1. QList<ExFOLDER *> mapi_folders;
    2. //....
    3. ExFOLDER * folder = new ExFOLDER(CRM_ID,oid);
    4. //...
    5. mapi_folders.append(folder);
    To copy to clipboard, switch view to plain text mode 

    after some procedures:
    Qt Code:
    1. while(mapi_folders.count() > 0)
    2. {
    3. ExFOLDER * folder = mapi_folders.takeFirst();
    4. //...
    5. delete folder;
    6. }
    To copy to clipboard, switch view to plain text mode 

    Do I have memory leaks in this code sample?

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

    Default Re: QList<pointer>::takeFirst() and memory leak question

    yes, if an exception is thrown in line 4; no otherwise.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QList<pointer>::takeFirst() and memory leak question

    Quote Originally Posted by caduel View Post
    yes, if an exception is thrown in line 4
    If you don't know exactly or if you want be absolutely sure, then use a normal iteration scope and afterward do
    Qt Code:
    1. qDeleteAll(mapi_folders);
    2. mapi_folders.clear();
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jun 2008
    Location
    Germany/Belarus/Sweden
    Posts
    53
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QList<pointer>::takeFirst() and memory leak question

    Quote Originally Posted by caduel View Post
    yes, if an exception is thrown in line 4; no otherwise.
    no exceptions for sure;

    If you don't know exactly or if you want be absolutely sure, then use a normal iteration scope and afterward do
    I have not much resources to wait for loopend... and do qDeleteAll(). WINCE Platform. Thought that Object removal when in it is no more necessity there, is good idea. The second(while(mapi_folders.count() > 0)) loop can take long time.

    Thanks for yours answers!

Similar Threads

  1. Memory leak detection
    By Sid in forum Qt Programming
    Replies: 8
    Last Post: 4th May 2011, 22:38
  2. memory leak question
    By cool_qt in forum General Programming
    Replies: 3
    Last Post: 20th January 2009, 07:49
  3. Memory leak weirdness
    By Darhuuk in forum General Programming
    Replies: 10
    Last Post: 10th January 2008, 18:51
  4. QPixMap and Memory leak
    By Krish_ng in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2007, 14:18
  5. Memory Leak in my Application :-(
    By Svaths in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 19:42

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.