Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm a c# programmer. And the thing I'm concern about is memory leak. Because it is critical in my program.

I know in this case:
obj1={ parent=null, child=obj2 }
obj2={ parent=obj1, child=null }
where obj1 is a global object and obj2 is a local object, this code:
obj1.child = null;

causes removing obj2.

But I'm doubtful if following code results removing obj2 and obj3 or not? because both of them still point each other:
obj1={ parent=null, child=obj2 }
obj2={ parent=obj1, child=obj3 }
obj3={ parent=obj2, child=null }
Posted

1 solution

obj1.child = null;
This code will decrease the reference count for obj2. The obj2 itself will be removed during the GC, when there are no references to it.
In the second case, if there are no more references to obj2 and obj3, setting obj1.child to null will cause removal of two objects: obj2 and obj3 because they will be considered orphaned object graph.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900