Event handlers

C

Claire

Hi,
Pardon my poor description ahead, I don't know the technical terms.
If I add an event to an event handler from a class that later goes "dead"
without being disposed, what happens when the event is fired?
My TestTemplate class doesnt support IDisposable and just relies on garbage
collection. In its constructor a checkbox is passed in the parameters to it.
I assign a new event hander to the checkbox checkedchanged event to a
function within the class.
I'm not sure whether that assignment would prevent the object being cleared
by GC as it's still held in the checkboxes' checkedchanged event list.
Is there any way to clear all event handlers waiting on an event for a
control?
I have several testtemplate classes that use various combinations of
controls on my form, depending on a test type that's being set up. Each
class has several controls passed to it via the constructor. Theres only
ever a single instance of one of my classes in use. In all cases except this
one, text boxes etc are databound to different fields of a test record and
it's very easy to break data bindings to a control.

I hope u understand me
 
J

Jon Skeet [C# MVP]

Pardon my poor description ahead, I don't know the technical terms.
If I add an event to an event handler from a class that later goes "dead"
without being disposed, what happens when the event is fired?

If an event has a handler which refers to an object, that object won't
be garbage collected.
My TestTemplate class doesnt support IDisposable and just relies on garbage
collection.

What do you need to happen on garbage collection? If you've got a
finalizer you expect to be called, I'd *strongly* recommend
implementing IDisposable.
In its constructor a checkbox is passed in the parameters to it.
I assign a new event hander to the checkbox checkedchanged event to a
function within the class.
I'm not sure whether that assignment would prevent the object being cleared
by GC as it's still held in the checkboxes' checkedchanged event list.
Yup.

Is there any way to clear all event handlers waiting on an event for a
control?

No - part of the encapsulation of an event is that you only have add/
remove access.
I have several testtemplate classes that use various combinations of
controls on my form, depending on a test type that's being set up. Each
class has several controls passed to it via the constructor. Theres only
ever a single instance of one of my classes in use. In all cases except this
one, text boxes etc are databound to different fields of a test record and
it's very easy to break data bindings to a control.

I hope u understand me

Well, I can't say I really understand your specific situation. Could
you post a short but complete example of the problem?

See http://pobox.com/~skeet/csharp/complete.html for more about what I
mean.

Jon
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi,
Pardon my poor description ahead, I don't know the technical terms.
If I add an event to an event handler from a class that later goes "dead"
without being disposed, what happens when the event is fired?

Very simple, it wont go dead in the first place :)
you are not holding a refeernce to it directly, but the instance that
will fire the event does. Now if this instance (the one with the
event) goes "dead" then your class can be collected.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top