rnicholus
asked on
Garbage collector question - regarding combo boxes
Ok, lets say I have a form with a ComboBox. And, let's say I've just created 4 objects. I then write code to add each of these objects to the combo box when a dropdown event is fired. That is, when the user clicks the dropdown portion of the ComboBox to display its contents, I have some code that adds each of my four objects <comboBox.Items.add(object 1); comboBox.Items.add(object2 ), etc...>. Now, lets say I create a button on my form that sets all four of these objects to null and I press this button after displaying the contents of the combo box. My question is, since I already displayed the contents of my combo box (which then created additional references to my 4 objects as it added them to the comboBox) will the GC be able to collect these 4 objects? I suspect not, but I want to be perfectly sure. If the answer is "no", then how to I get fix this situation? (I ask this because my example is a very simple version of a very large project I am working on).
To make sure my point is clear, here is some pseudocode:
Object object1 = new Object();
Object object2 = new Object();
Object object3 = new Object();
Object object4 = new Object();
//event handler for combo box drop-down event
comboBox.Items.add(object1 );
comboBox.Items.add(object2 );
comboBox.Items.add(object3 );
comboBox.Items.add(object4 );
//event handler for click event attached to button that nulls all objects
object1 = null;
object2 = null;
object3 = null;
object4 = null;
To make sure my point is clear, here is some pseudocode:
Object object1 = new Object();
Object object2 = new Object();
Object object3 = new Object();
Object object4 = new Object();
//event handler for combo box drop-down event
comboBox.Items.add(object1
comboBox.Items.add(object2
comboBox.Items.add(object3
comboBox.Items.add(object4
//event handler for click event attached to button that nulls all objects
object1 = null;
object2 = null;
object3 = null;
object4 = null;
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Like itsvtk said, no, they won't be GC'd.
If you want that to happen, you might try to insert a clone of the object instead of the object itself? Let your object inherit the IClonable interface, call the clone() method and add the cloned object to the combobox. Or, add a Clone() method to your object and return this.MemberWiseClone(). Please note, though, that these are shallow copies and I don't know if that is a problem.
HTH,
Razzie
If you want that to happen, you might try to insert a clone of the object instead of the object itself? Let your object inherit the IClonable interface, call the clone() method and add the cloned object to the combobox. Or, add a Clone() method to your object and return this.MemberWiseClone(). Please note, though, that these are shallow copies and I don't know if that is a problem.
HTH,
Razzie
ASKER
Wait a minute, you mean to tell me that if I clear the combobBox and then nullify the objects, the GC won't collect? I find this hard to beleive. If I clear the comboBox, the comboBox no longer contains any references to these objects. If I am wrong, can you please explain further?
Unforntunately, anything other than a reference will not work. Each object is a wrapper for a running process.
Unforntunately, anything other than a reference will not work. Each object is a wrapper for a running process.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
However, I figure clearing the comboBox after it goes out of focus may help this problem, since, after clearing the combobox, the references no longer exist. Does this sound like a sold idea?