CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2002
    Posts
    33

    Memory management with circular reference

    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

  2. #2
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    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?
    Succinct is verbose for terse

  3. #3
    Join Date
    Sep 2002
    Posts
    33
    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

  4. #4
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    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.
    Succinct is verbose for terse

  5. #5
    Join Date
    Sep 2002
    Posts
    33
    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).

  6. #6
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    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.
    Succinct is verbose for terse

  7. #7
    Join Date
    Sep 2002
    Posts
    33
    That sounds good Cup, thank you very much for the help.

  8. #8
    Join Date
    Aug 2002
    Posts
    78
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured