Page 1 of 1

MemProfiler and VB.NET Handles clause

Posted: Tue Jul 26, 2011 6:43 pm
by sparky2708
I keep getting MemProfiler reporting that I don't unsubscribe from events. The thing is that our code in VB.NET has the "Handles <EventXXX>" clause on some methods and it looks like that is what MemProfiler is reporting as a leak. When does the "Handles" clause unsubscribe? Why is MemProfiler reporting this as a leak?

Re: MemProfiler and VB.NET Handles clause

Posted: Wed Jul 27, 2011 1:20 pm
by Andreas Suurkuusk
The Handles clause will never unsubscribe, but normally this is not a problem. If the Handles clause is added by a form designer, the subscriber and the event source usually have the same life time. For instance, a Form might handle a button event and when the form is closed, both the form and the button will be eligible for garbage collection. In this case, there's no need to remove the handler. On the other hand, if the event source has a longer lifetime than the subscriber, then the event handler must be removed manually by using the RemoveHandler keyword.

.NET Memory Profiler will give you information about event handlers that solely responsible for keeping an instance alive, as this is a common cause of memory leaks (but definitely not a certain memory leak).

What kind of issue does the profiler report?