Click to See Complete Forum and Search --> : Memory management with circular reference


TruBic
September 25th, 2002, 04:55 PM
Hi Guys

I got an unsolved problems with memory management and circular reference. Memory management means an object have to destroy itself when it's no longer used. Circular reference is the case when two object reference to each other either directly or indirectly.

The real case is I have a collection which hold a list of children. The children can be the object itself or the object's ancesstor. When a collection is deleted, it tries to delete all it's children and the children in turn try to delete the children of themselves. The probelm happens here because that cause an infinite recursive loop and object could be deleted more than one.

Anybody have an idea of how to solve it?

Thanks

cup
September 26th, 2002, 12:52 AM
Could you add a count of how many parents each child has, decrement whenever the child is meant to be deleted and only delete the child when it has no more parents?

TruBic
September 26th, 2002, 12:43 PM
It might be a good idea Cup, but when should I decrement the parent count of a child ? When it is removed from its parent or when it is released by an user ?

Ex: I have a hold to pA and pB, none of them parent or child. Then I do something like pA.Add(pB) and pB.Add(pA), after using them for a while, I release both of them and expect they are automatic deleted!

How to apply your solution to that case ?

Thanks

cup
September 27th, 2002, 02:25 AM
Increment the count on addition and decrement the count on removal. Instead of deleting explicitly, delete this in the removal routine, when the count is zero.

The only problem with this is: are you likely to create objects that will not be added to anything. Those will not get deleted by the remove routine and will have to be removed explicitly.

TruBic
September 27th, 2002, 02:39 PM
The nature of the probjem is like that. Because an object can be independent from others. It has no child or parent until some children add to it or it's added to some parent.

And the point of the problem is no matter whether an object is an independent or dependent object, it should be treated the same (i.e knows how to free it resource before actually deleted).

cup
September 27th, 2002, 02:48 PM
Here's an alternative:

Store a static list of instances for the cleanup operation of all the independent objects at the end. Whenever an object is created, it is added to the list, whenever it is deleted, it is removed. You still keep the counters to tell you when to delete. At the end of the program, the ones remaining in the list get wiped.

TruBic
September 27th, 2002, 03:09 PM
That sounds good Cup, thank you very much for the help.

Gorgor
October 2nd, 2002, 11:47 AM
If you don't mind wasting a few extra CPU cycles, and since it sounds like you're blowing away an entire tree at once, you could start at the top, check top's pointer, run thru the tree NULLing the pointers that == top's, then blow away top and recurse on each child (storing their pointers before clobbering top.)

Should be safe (won't lose any branches) because each child will only lose pointers to things that are the next thing to be deleted, thus the remaining tree should remain contiguous.