Zombie objects in c# and .net what they are and how to avoid them

.net, asp.net, asp.net-mvc, c#, winforms

Solution

The event-caused zombie situation is this:

class AnObjectThatWillSoonGoOutOfScope{
  public AnObjectThatWillSoonGoOutOfScope(ISomeLongLivedService service){
    service.SomeEvent += OnSomeEvent;
  }
  private void OnSomeEvent(...){...}
}

The long-time service keeps a reference to the child object that should have unsubscribed from the event before going out of scope. You can use a dispose pattern to avoid this scenario. You can use a tool like the Ants Memory Profiler to track these down. Generally the problem does not exist if you are subscribing to events on your own instance (because both the subscribee and subscriber will be available for garbage collection).

Problem

Lately I had a question on an interview about Zombie Objects in c#. Could you kindly explain to me by using a simple example what they are ? The zombie objects in that interview were related to applications regarding Win-forms, can we get these objects in asp.net MVC for example ? Thank you for taking the time to explain, because I have searched for it and i did not find a example or an explanation that I could understand

Original source

Related problems