all groups > dotnet clr > july 2003 >
You're in the

dotnet clr

group:

Events: Do they lead to memory leaks?


Re: Events: Do they lead to memory leaks? David Browne
7/25/2003 9:16:21 AM
dotnet clr:
[quoted text, click to view]

Yes

[quoted text, click to view]

It will not be garbage collected.

David

Events: Do they lead to memory leaks? Rahul Kumar
7/25/2003 10:09:31 AM
Hi all

I have the following scenario in brief: There are two classes EventRaiser
and EventConsumer. The EventRaiser raises an event and EventConsumer listens
to it, by adding its handler to the event chain of the EventRaiser's event
delegate. My question is: If the EventConsumer goes out of context, without
removing itself from the event chain, does the EventRaiser still have its
reference through the event chain? Will it not be garbage collected?

Regards

RK

Re: Events: Do they lead to memory leaks? David Browne
7/25/2003 11:35:23 AM

[quoted text, click to view]

Well that's not how events are implemented, but they could have been.

If you used interfaces instead of events, you could use weak references to
notitify the object without keeping it from being collected.

But only good programming prevents this. You might make your objects
Disposable, and remove the event handler in the Dispose method.

David

Re: Events: Do they lead to memory leaks? Rahul Kumar
7/25/2003 4:58:45 PM
Hi David

Thanks for the response. I have got few more questions for you:

How to take care of the memory leaks in such cases?
Are there in safeguards in .Net against it, except good programming
practices?
How about using WeakReferences to link the two Event objects?

Regards

Rahul


"David Browne" <davidbaxterbrowne no potted meat@hotmail.com> wrote in
message news:ekpBUdrUDHA.2192@TK2MSFTNGP09.phx.gbl...
[quoted text, click to view]

Re: Events: Do they lead to memory leaks? Niall
7/28/2003 10:38:39 AM
Weak references could have worked in this situation. Bear in mind that it's
only really an issue when your object is hooking onto an event in another
object which has a significantly longer life span. But it can be a really
nasty situation, especially if it's not you hooking up the event, but
something in the framework, and your object ends up being referenced through
a static hashtable or similar.

Niall

[quoted text, click to view]

AddThis Social Bookmark Button