views:

54

answers:

2

I am writting a software in Silverlight with C#.

I know that GC.Collect will collect the Objects and Controls if they are non-referenced. But in detail, I am not so sure how is exactly means by non-reference.

I know that in Silverlight, I have to remove the Control (say called "Control A") from the Layout, take out all the event handler, then set the object to null, so it not reference to the object. So something like:

But I mean like...

1) if the "Control A" it contains other Controls: "Control B", "Control C", and they might have subscripted event handler somewhere. Will the "Control A" still get collected by GC.Collect()? And how about "Contorl B" "Control C"? Do I have to actually remove everything that "Control B" and "Control C" that contains and remove "Control B" "Control C" from "Control A" to let them get collected?

2) Say if there is a "Control D" continas a ComboBox, and the ComboBox has a lot ComboxBoxItem. Do I have to Clear() out all the ComboxBoxItem so those ComboxBoxItem will be collected? Or when I remove ComboBox from "Control D", will take out ComboxBoxItem too?

I am kind of confused with Delete in C++, since in C++ I can just delete the whole object with everything it contains...

Thanks

+1  A: 

An object will only be collected if it is not referenced (directly or indirectly) by a root variable that is in scope (either on the stack or static). In other words, if A references B and B references A, they'll both get collected.

From http://msdn.microsoft.com/en-us/library/ee787088.aspx:

The garbage collector uses the following information to determine whether objects are live:

Stack roots. Stack variables provided by the just-in-time (JIT) compiler and stack walker.

Garbage collection handles. Handles that point to managed objects and that can be allocated by user code or by the common language runtime.

Static data. Static objects in application domains that could be referencing other objects. Each application domain keeps track of its static objects.

Steven Sudit
actually I read that before, but I don't really understand... that's why I ask here
King
Well, hopefully the explanation made the quote make sense.
Steven Sudit
+2  A: 

If Control A or any contained controls have events, and some other classes subscribed to these events, this means: Control A (B,C) have reference to another class (subscriber). This doesn't prevent these controls to be collected.

If Control A (B, C) subscribed to some event of class D, this means, class D has reference to A (B, C). This prevents these controls to be collected.

Regaring internal mutual references between control A and its children, GC is smart enough to recognize this and collect all of them.

Alex Farber
Thanks a lot, this is what I want to know. Because all my controls is splited into many small controls and put them together, so if I have to remove them one by one is a big pain :)
King
@Alex Farber: Although it isn't strictly necessary for an event subscriber to hold a reference to the object tom whose events it has subscribed, isn't it normal for an event subscriber to do so? Some event subscribers may use a weak reference to avoid keeping alive the object to which they're subscribed, but without some kind of reference how would a subscriber unsubscribe itself?
supercat
supercat: I don't know much about weak references and prefer to unsubscribe when necessary: my main language is C++ and I never forget to make cleanup operations :)
Alex Farber