Results 1 to 4 of 4

Thread: Garbabe Collection

  1. #1
    Join Date
    Oct 2007
    Posts
    78
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Garbabe Collection

    Not sure how to proceed and before getting in too deep I have a few questions. I will start with a small code example.

    Qt Code:
    1. typedef struct mystructure {
    2. int x;
    3.  
    4. QList<mystructure> children;
    5. } mystructure;
    6.  
    7. QList<mystructure> mystructureList; // List of parent QTreeWidgetItem(s) only need these items as children references will be contained in mystructure
    8. QHash<QTreeWidgetItem *,mystructure> mystructureFromItem; // lookup hash using QTreeWidgetItem index for mystructure
    To copy to clipboard, switch view to plain text mode 

    My problem arises since I am not using pointers if I have already appended a mystructure to the QList and add more children after that fact they will not be stored.

    If I Q_DECLARE_METATYPE(mystructure) I would have to implement == functionality for QList in order to use indexOf() features to just replace the entry with the updated data.

    Should I just use a standard pointer linked list instead of forcing this into Qt mechanics? Or is there just something else I am missing?

    Bob

    EDIT: Forgot to add the question of making QList<mystructure> *mystructureList, and QList<mystructure> *children. In this case would I need to do my own memory release or would Qt's garbage collection take care of it?
    Last edited by coderbob; 19th November 2007 at 21:56.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Garbabe Collection

    Why aren't you using pointers to mystructure? Internally, QList stores the items as pointers to the items you add. Since you are creating your mystructure objects on the stack of some function and you add them to the list, after their scope ends the QList will hold invalid references.

    So, use pointers!

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Garbabe Collection

    Quote Originally Posted by coderbob View Post
    My problem arises since I am not using pointers if I have already appended a mystructure to the QList and add more children after that fact they will not be stored.
    Since you have a list of stuctures, not pointers, you can't store original object in the list. Instead, each time you add something to the list, a copy is created, so you have to change that copy.

    Quote Originally Posted by coderbob View Post
    In this case would I need to do my own memory release or would Qt's garbage collection take care of it?
    You will have to delete them yourself. Qt deletes only objects it owns (like events) and children of deleted QObjects.

  4. #4
    Join Date
    Oct 2007
    Posts
    78
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Garbabe Collection

    Thank you for the responses,

    marcel, It is only a temporary tracking structure but having to worry about scope should be avoided and makes sense.

    jacek, Sorry I went off on another line of thought and had to re-edit the original post about using pointers in a QList if I would need to do my own garbage collection or if Qt would handle it.

    Bob

Similar Threads

  1. Resource Collection File maximux size
    By Lele in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2006, 11:41
  2. lib using Image Collection
    By bitChanger in forum Qt Programming
    Replies: 4
    Last Post: 25th July 2006, 18:30

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.