?
Solved

Garbage collector question - regarding combo boxes

Posted on 2005-03-04
5
Medium Priority
?
242 Views
Last Modified: 2012-05-05
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(object1); 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;





0
Comment
Question by:rnicholus
  • 2
  • 2
5 Comments
 
LVL 14

Accepted Solution

by:
Thandava Vallepalli earned 1400 total points
ID: 13464787
will the GC be able to collect these 4 objects?
No

To recycle the objects, objects should not be referenced by any of the variables in your project. then only GC will re-cycle them

in ur application you already assiged them to combobox, internally combobox will refere these objects.  

If you do like below then ur objects gets re-cycled by GC


================
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.ToString());
comboBox.Items.add(object2.ToString());
comboBox.Items.add(object3.ToString());
comboBox.Items.add(object4.ToString());

//event handler for click event attached to button that nulls all objects
object1 = null;
object2 = null;
object3 = null;
object4 = null;

=============================

itsvtk
0
 

Author Comment

by:rnicholus
ID: 13465016
Actually, I need to insert the actual objects in the comboBox.  Otherwise I don't see a way to access the object from the comboBox.  My app is setup so the user can select an object from the combobox and display its properties.  I'm not aware of a good way to do this other than inserting the object itself into the box.  Is there?

 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?

0
 
LVL 8

Expert Comment

by:Razzie_
ID: 13467094
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
0
 

Author Comment

by:rnicholus
ID: 13467205
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.  
0
 
LVL 8

Assisted Solution

by:Razzie_
Razzie_ earned 600 total points
ID: 13467468
Hmm no I didn't mean that, if you clear the combobox and it holds on more references to the objects then yes, the objects will be GC'ed, yes. My answer was more a reply to your first post :)

If you can't clone the objects then clearing the combox would be the easiest way to go I guess.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question