?
Solved

Garbage collector question - regarding combo boxes

Posted on 2005-03-04
5
Medium Priority
?
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

752 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