PDA

View Full Version : Garbabe Collection



coderbob
19th November 2007, 20:43
Not sure how to proceed and before getting in too deep I have a few questions. I will start with a small code example.



typedef struct mystructure {
QTreeWidgetItem *item;
int x;

QList<mystructure> children;
} mystructure;

QList<mystructure> mystructureList; // List of parent QTreeWidgetItem(s) only need these items as children references will be contained in mystructure
QHash<QTreeWidgetItem *,mystructure> mystructureFromItem; // lookup hash using QTreeWidgetItem index for mystructure



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?

marcel
19th November 2007, 20:55
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!

jacek
19th November 2007, 20:58
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.


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.

coderbob
19th November 2007, 21:02
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