Re: Events unsubscribing and resource leaks?



Hello

I never talked of sender receiver so, i presume you are in agreement that
events do cause resource leaks if not unsubscribed from?

My issue is that my sender is a singleton class. And my receiver is an
instance created inside another singleton.

Even after unsubscribing all events in my receiver instance inside the
singleton the instance still will not be garbage collected, even with a
forced dispose.

I presume this is due to the singleton that it is in remaining? But that
does not make much sense to me as to why that would be.


"Christof Nordiek" <cn@xxxxxxxxx> wrote in message
news:OOcxd9s4GHA.4832@xxxxxxxxxxxxxxxxxxxxxxx
Hi Daniel,
see inline.

"Daniel" <DanielV@xxxxxxxxxxxxxxxx> schrieb im Newsbeitrag
news:u74hSwp4GHA.2348@xxxxxxxxxxxxxxxxxxxxxxx
Hey guys

When you hook an event (c# 2.0 syntax): myEvent += MyMethodToFire;

You need to also unsubscribe it to avoid a resource leak so that the
object it is in gets garbage collected like so : myEvent -=
MyMethodToFire;

Are you sure?

The event will prevent the reciever of the event (if the method is an
instance method) to be collected, not the sender. If the sende is the
reciever, this also wouldn't prevent the instance from being collected. So
where is the resource leak?
The event should only be unsubscribed, if the reciever shall ciece to
recieve the event while the sender remains to exist (and fireing the
event).


That's all fine, but when you use visual studio to create events for
objects it never creates an unsubscribing reference, so is it puting in
resource leaks? Or is this being cleared somewhere that i am not seeing?

Since the sender will be contained by the sender, i don't think this cuold
be a problem.


Also what is the best way to clean up a singleton class?





.


Loading