Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5947451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:57:58+00:00 2026-05-22T16:57:58+00:00

I am currently debugging a big (very big!) C# application that contains memory leaks.

  • 0

I am currently debugging a big (very big!) C# application that contains memory leaks. It mainly uses Winforms for the GUI, though a couple of controls are made in WPF and hosted with an ElementHost. Until now, I have found that many of the memory leaks were caused by events not being unhooked (by calling -=) and I’ve been able to solve the problem.

However, I just came across a similar problem. There is a class called WorkItem (short lived) which in the constructor registers to events of another class called ClientEntityCache (long lived). The events were never unhooked and I could see in .NET profiler that instances of WorkItem were being kept alive when they shouldn’t because of those events. So I decided to make WorkItem implement IDisposable and in the Dispose() function I unhook the events this way:

public void Dispose()
{
  ClientEntityCache.EntityCacheCleared -= ClientEntityCache_CacheCleared;
  // Same thing for 10 other events
}

EDIT

Here is the code I use for subscription:

public WorkItem()
{
  ClientEntityCache.EntityCacheCleared += ClientEntityCache_CacheCleared;
  // Same thing for 10 other events
}

I also changed the code for unregistering to not call new EntityCacheClearedEventHandler.

END OF EDIT

I made the calls to Dispose at the proper places in the code that uses WorkItem and when I debug I can see that the function is really being called and I do -= for every event. But I still get a memory leak and my WorkItems still stay alive after being Disposed and in .NET profiler I can see that the instances are kept alive because the event handlers (like EntityCacheClearedEventHandler) still have them in their invocation list. I tried to unhook them more than once (multiple -=) just to make sure they were not hooked more than once but this doesn’t help.

Anyone has an idea why this is happening or what I could do to solve the problem?
I suppose I could change the event handlers to use weak delegates but this would require to mess a lot with a big pile of legacy code.

Thanks!

EDIT:

If this helps, here is the root path described by .NET profiler:
lots of things point on ClientEntityCache, which points to EntityCacheClearedEventHandler, which points to Object[], which points to another instance of EntityCacheClearedEventHandler (I don’t understand why), which points to WorkItem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:57:59+00:00Added an answer on May 22, 2026 at 4:57 pm

    It might be that multiple different delegate functions are wired to the event. Hopefully the following little example will make it clearer as to what I mean.

    // Simple class to host the Event
    class Test
    {
      public event EventHandler MyEvent;
    }
    
    // Two different methods which will be wired to the Event
    static void MyEventHandler1(object sender, EventArgs e)
    {
      throw new NotImplementedException();
    }
    
    static void MyEventHandler2(object sender, EventArgs e)
    {
      throw new NotImplementedException();
    }
    
    
    [STAThread]
    static void Main(string[] args)
    {
      Test t = new Test();
      t.MyEvent += new EventHandler(MyEventHandler1);
      t.MyEvent += new EventHandler(MyEventHandler2); 
    
      // Break here before removing the event handler and inspect t.MyEvent
    
      t.MyEvent -= new EventHandler(MyEventHandler1);      
      t.MyEvent -= new EventHandler(MyEventHandler1);  // Note this is again MyEventHandler1    
    }
    

    If you break before the removal of the event handler you can view the invocation list in the debugger. See below, there are 2 handlers, one for MyEventHandler1 and another for the method MyEventHandler2.

    enter image description here

    Now after removing the MyEventHandler1 twice, MyEventHandler2 is still registered, because there is only one delegate left it looks a little different, it is no longer showing in the list, but until the delegate for MyEventHandler2 is removed it will still be referenced by the event.

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: I'm currently debugging an application written over a custom-built GUI framework in C++.
I'm currently debugging an ASP.NET MVC application that was just deployed to a different
I'm currently debugging a project that uses an external library ( LibFirm ). When
I am currently debugging an application which uses pthreads. When I attach GDB it
I would like to see the threads currently active in my application while debugging
I am currently debugging someone else's code that flips the ups and downs relative
When debugging a VB6 application, I have noticed that the VB6 IDE keeps any
I'm debugging an ASP.NET MVC 2 application running on .NET 4.0 (Windows 7), currently
I am currently debugging code that sometimes works and sometimes does not. It is
I am currently debugging a script that constantly runs into OutOfMemory exceptions. It is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.