MemProfiler and VB.NET Handles clause

Use this forum to discuss subjects that don't belong in the other forums.
Post Reply
Posts: 4
Joined: Sat Jan 22, 2011 5:14 am

MemProfiler and VB.NET Handles clause

Post by sparky2708 » Tue Jul 26, 2011 6:43 pm

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?

Andreas Suurkuusk
Posts: 1030
Joined: Wed Mar 02, 2005 7:53 pm

Re: MemProfiler and VB.NET Handles clause

Post by Andreas Suurkuusk » Wed Jul 27, 2011 1:20 pm

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?
Best regards,

Andreas Suurkuusk
SciTech Software AB

Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests