Delegates Subscribe and Unscubscribe

J

Joe

I have a usercontrol which has an event called NameChanged; The form that
uses the control subscribes to NameChanged.
The first time this form is invoked everything works fine. The second time I
invoke the form it seems theat NameChanged is still holdng a reference to
the first call so that one gets called first and throws an exception about
the object is already disposed.

My question is: Why does the event not get automatically unsubscribed when
the usercontrol is destroyed? Is it required to manually unsubscribe every
event when disposing of an object?
 
M

Marc Gravell

Can you be a little clearer about what subscribes to what?

To expand, if object A has an event, and object B subscribes (using an
instance method on B), then A can see B through the event, but not the other
way around.

The usual mistake here is not controls but data entities; e.g. a Customer
object has an event, which a form subscribes to. If that form doesn't
unsubscribe when closing, then the Customer object will actually prevent the
form from being garbage collected. And when the event fires it *will*
attempt to invoke the method on the form, which will usually fail.

In your example you had a control and a form, and I'm guessing that their
lifetime is very similar, so I'm not sure what issue you are seeing. In
particular, it would be unusual for the UserControl to live longer than the
parent Form, so I am confused as to why the event on the UserControl is
causing an issue... unless the UserControl is bound (in turn) to a regular
class as per the previous paragraph.

But the short answer is yes: when an object is cleanly committing suicide it
should unsubscribe from any objects that it reasonably expects to live on
without it - primarily data entities that exist independently of the forms
in an "observer" scenario. Normally child controls don't fall into this
category, as the child control will usually die with the parent. Note that
if you are being finalized (~MyClass) by the GC you shouldn't attempt to do
*anything* with managed references... it could all go horribly wrong.

Marc
 
G

Guest

Is the event or anything else about the user control static. If the first
form has been disposed then the usercontrol should have fallen out of scope
too, but a new form instance should have a new usercontrol instance on it
anyway.
Look to see if you have static references around the event somewhere


Ciaran O'Donnell
 
J

Jon Skeet [C# MVP]

Joe said:
I have a usercontrol which has an event called NameChanged; The form that
uses the control subscribes to NameChanged.
The first time this form is invoked everything works fine. The second time I
invoke the form it seems theat NameChanged is still holdng a reference to
the first call so that one gets called first and throws an exception about
the object is already disposed.

My question is: Why does the event not get automatically unsubscribed when
the usercontrol is destroyed? Is it required to manually unsubscribe every
event when disposing of an object?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
L

Linda Liu [MSFT]

Hi Joe,

When the form is shown for the second time, is it a new instance that is
different from the previous instance? Is so, the UserControl on the new
form is a new instance as well. Then the NameChanged event of the new
usercontrol will only hold a reference to the event handler in the new
form, unless the NameChanged event is static.

I performed a test based on your description but didn't produce the
problem. The following is the steps of my test.

1. Create a WindForms application project. Add a UserControl and a form
called From2 in the project.
2. Add a static event to the UserControl. Add a button on the UserControl
to trigger this event.
3. Build the project. Add an instance of the UserControl on Form2 and
subscribe the event of the UserControl in the Form2's constructor.
4. Add a button on Form1. In the button's Click event handler, create a new
instance of Form2 and show it.

Run the program. When I click the button on the Form1 for the first time, a
Form2 appears. I click the button on the Form2 and the event handling
method in the Form2 is executed. Close the Form2. When I click the button
on the Form1 for the second time, a new Form2 instance appears. I click the
button on the new Form2 and the event handling method is executed twice,
which means that the event delegate of the UserControl still holds the
reference to the event handling method in the first Form2 instance. I don't
encounter any exception.

Actually, there's another case that the event receiver is disposed before
the event sender. In this case, when the event is fired, the event delegate
still holds the reference to the event handling method in the event
receiver. For example, there're two forms called Form1 and Form2. Form1 has
an event and Form2 subscribe this event. When the two forms are shown, all
works fine. Now close the Form2 and then trigger the event of Form1. The
event handling method in Form2 is still be executed without any exception.

As for the first case, we should unsubscribe the event of the UserControl
in the form when the form is closed.

As for the second case, we should unsubscribe the event of the Form1 in the
Form2 when the Form2 is closed.

Hope this helps.

If you need our further assistance, please send me your sample project that
could just reproduce the problem. To get the actual email address, remove
'online' from my displayed email address.



Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Linda Liu [MSFT]

Hi Joe,

How about the problem now?

If the problem has not resolved or you need our further assistance, please
feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!


Sincerely,
Linda Liu
Microsoft Online Community Support
 

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