Click here to Skip to main content
15,920,468 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I was wondering if you could give me a hint on what's the best way of approaching this ..

I have a custom control(class) (It's actually a button) that I initialize multiple times during run time..
Currently to apply changes to the buttons, I need to restart the application - but now I'm getting tired of doing so - therefore I would prefer to remove all Instances of the class (including the handles that I initialized during creation) and then clear new instances after wards with the new settings

What would be the best approach to do so ??
Should I just hold a ref to the control in a hash table or collection and then just clear them from there ?

I also thought there was a function that returns all initialized instances of a class - but I could not find anything pointing in that direction anymore

Any Imput would be appropriated
Sergey Alexandrovich Kryukov 19-Jan-12 0:32am    
The question is not fully formulated. I want to know if this is 1) WPF application, 2) Forms application, 3) any other kind of application?
...actually, forget it. Sorry for the confusion. Please see my answer below.

1 solution

Basically, the best approach is collecting all of the instances in some container. Wait a minute, not so fast. There is a possibility that your thinking is to abuse the garbage-collected architecture. But maybe not. Let's sort it out.

You cannot just "remove instance", as there is no such thing in managed application. You only need to remove the referenced to the instances, and they are, again, are stored in some container. So, in the simplest case, you simply need to clear the container.

I hope you already understand this, but it need some clarification. Any object on heap will be destructed and removed from memory by the Garbage Collector (GC) if your object lost its reachability. Reachability is not a simple concept at all. For example, if three objects are referencing each other in cycle (A -> B -> C -> A), but there no more references to either of them, they will be garbage-collected anyway.

I can imagine that your picture is more complex. There are many different containers, nesting containers, each containing objects of different types, polymorphous or not. Usually, you should recursively traverse all your containers and clear them. This is the best way.

Let's imaging that this is difficult for you, by whatever reason. Then you could collect all the child-parent relationship to be cleared in some separate container. The storing is not trivial; you will need to store some data and references needed to remove an object from its parent container. Maybe, it should be just the collection of containers. It depends on your design which I don't know. My thing is to give you the right idea.

The problem here is this: this extra collection itself will hold objects and thus will keep them from collection. There are two ways to work around this problem. 1) You will need to clean this extra collection itself when the work is done; 2) you can hold not "real" references, but "weak references", which can be used for referencing by will allow garbage collection.

The problem of weak referencing is resolved using the class System.WeakReference, please see[^].

I would also recommend to read the interesting articles by CodeProject author Paulo Zemec:
Creating a Weak List[^],
WeakReferences, GCHandles, and WeakArrays[^],
Creating a weak event[^].

Share this answer
Georg Kohler 19-Jan-12 1:13am    
I had the feling it won't be that easy - I think i get the idea ...
I'll work on it tommorrow
you mentione quote : "and they are, again, are stored in some container"
Question - any idea how to locate this "container" - is it somehow "related" to the base class (that's what i remember , that there is a way to get a list of all instances of a particular class ....) But I might be wrong :-)

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