Groups | Blog | Home
all groups > dotnet framework > november 2006 >

dotnet framework : Disposing EventHandlers



Mike Carlisle
11/27/2006 8:46:01 PM
Hi,

In a windows app is it necessary to explicity release delegates from events
by implementing dispose, or should the framework take care of this?

I've read that you only need to implement IDisposable when handling
unmanaged resources, which makes sence... however...

Investigating a memory leak in a large application there are over 80,000
referrences to a single button in the GC after 30 mins runtime. It seems that
not implementing dispose and releasing the references causes this. Can
someone confirm?

Thanks,

Mattias Sjögren
11/28/2006 9:40:10 PM
Mike,

[quoted text, click to view]

If you want the object handling the event to be eligible for garbage
collection before the event provider is, you have to remove the
handler from the event. Whether that removal code should go in a
Dispose method or somewhere else depends on your class design.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
AddThis Social Bookmark Button