Results 1 to 4 of 4

Thread: proper use of QList?

  1. #1
    Join Date
    Sep 2008
    Location
    Falmouth, MA, USA
    Posts
    34
    Thanks
    4
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default proper use of QList?

    I am trying to implement a history of structures--I want to maintain a list of them, with a given maximum size. As a new structure arrives, I want to remove the oldest, and add the new one. In other words, a FIFO.

    I am using a QList. Until I reach my maximum size, as new data arrives, I malloc a structure, fill it, and list.prepend() the pointer to the list. Forgive me for mallocing structures instead of creating new objects, I am at heart a C programmer, and this is legacy code being fit into a new QT app.

    Once I reach my maximum size, as new data arrives, I use list.takeLast(), and use the returned data (the pointer to my previously malloc'd structure) for my new data. I prepend that to the list. I am trying to avoid continual new memory allocation, as these operations happen quickly, and my program typically runs for many days.

    While this appears to do what I want, I also seem to have a memory leak. Why would this be? Am I doing something really stupid?

    Thanks for any help.

  2. #2
    Join Date
    Jun 2010
    Posts
    7
    Platforms
    Unix/X11 Windows

    Default Re: proper use of QList?

    Hi,
    you remove item from the qlist which is a pointer,the qlist only removes the pointer,but the data of pointer have not been released,so cause memory leak

  3. #3
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: proper use of QList?

    @foxhengxing: That's not it. He is reusing the structure for the new data and prepends it. The whole thing is a preallocated cyclic list.

    I am not sure, but the way I see it, you are moving the list always backwards, so QList needs to expand its internal array. QList is not a pointerlist! Do you need access by index? Look into QLinkedList if not.

    Maybe you could also use QContiguousCache? Seems to be optimized for the paired appending/prepending. In combination with your preallocated data pointers that could do the trick.

    For testing purposes you could make your structure really big and see if your memory leak scales with it.

    Joh

  4. The following user says thank you to JohannesMunk for this useful post:

    jhowland (13th September 2010)

  5. #4
    Join Date
    Sep 2008
    Location
    Falmouth, MA, USA
    Posts
    34
    Thanks
    4
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: proper use of QList?

    Joh:

    I was unfamiliar with QContinuousCache, and you are right, it seems to be exactly what I need to use--thanks very much.

Similar Threads

  1. Replies: 4
    Last Post: 20th August 2010, 13:54
  2. Proper QList usage
    By space_otter in forum Newbie
    Replies: 5
    Last Post: 22nd June 2010, 06:57
  3. Copying an QList into a new, sorted QList.
    By Thomas Wrobel in forum Newbie
    Replies: 3
    Last Post: 11th January 2010, 18:27
  4. QList: Out of memory - without having defined QList
    By miroslav_karpis in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2009, 08:42
  5. Proper way to #include Qt?
    By pherthyl in forum Qt Programming
    Replies: 1
    Last Post: 11th August 2006, 02:15

Tags for this Thread

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.