Fix a leak (event type) in a .NET application

I have a windows form application written in .NET 4.0. Recently, during some tests, I noticed that there is some problem with the descriptors. The table below shows the results:

enter image description here

As you can see, the only type of type that is incremented is the event.

So my question is ... is it possible that the described problem is caused by the Windows forms application? I mean that I do not synchronize streams using AutoResetEvent or ManualResetEvent. I use streams, but what can be seen from the table above the number of thread handles seems to be good, so I assume that they are well controlled with the CLR?

Can I coat any third-party components that I also use in my application?

If I am unclear, I will try to answer your questions. Thanks for the help!

+7
multithreading c # memory-leaks thread-synchronization
source share
1 answer

Events are the main source of memory leaks in .Net, and AutoResetEvent and ManualResetEvent very poorly named. They are not the cause.

When you see something like this:

 myForm.OnClicked += Form_ClickHandler 

This is the type of event in question. When you register an event handler, the event source (for example, OnClicked ) keeps a reference to the handler. If you create and register new handlers, you MUST deregister the event (for example, myForm.OnClicked -= Form_ClickHandler ), otherwise your memory usage will increase.

For more information:

  • Why and how to avoid memory leaks of the event handler?
  • C # memory leak events
+2
source share

All Articles