M
Michal Talaga
Hello!
In windows forms application I have an object MyObject with an event
SomeEvent.
I pass this object to a form to display its data. In this form I attach an
event hadler for SomeEvent.
Now, I close the first form and then the form goes out of scope, and pass
MyObject to some other form, which also attaches handler to SomeEvent.
Now the problem is, even if the first form is closed/deleted (not hidden),
it will not be garbage collected, because of the reference from MyObject
(through the event handler).
Is there a better way to solve this problem, other than manually removing
event handlers from MyObject.SomeEvent when closing the form?
It is of course a bigger problem than the situation described and having to
remember to remove hadlers from events is error prone.
Any advice? I have a feeling that I'm missing something.
In windows forms application I have an object MyObject with an event
SomeEvent.
I pass this object to a form to display its data. In this form I attach an
event hadler for SomeEvent.
Now, I close the first form and then the form goes out of scope, and pass
MyObject to some other form, which also attaches handler to SomeEvent.
Now the problem is, even if the first form is closed/deleted (not hidden),
it will not be garbage collected, because of the reference from MyObject
(through the event handler).
Is there a better way to solve this problem, other than manually removing
event handlers from MyObject.SomeEvent when closing the form?
It is of course a bigger problem than the situation described and having to
remember to remove hadlers from events is error prone.
Any advice? I have a feeling that I'm missing something.