4

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

10
  • 2
    Here's a Similar Question Commented Feb 3, 2014 at 14:26
  • I would imagine they were referring to the event handler memory leak problem? Commented Feb 3, 2014 at 14:41
  • If you can get zombie objects in .net, you can get them in any .Net application, forms, asp, wpf, everywhere. Because they would be related to framework, not a specific language Commented Feb 3, 2014 at 14:46
  • 4
    Zombies are un-dead creatures who intend to eat people's brains (and plants). You can eliminate your zombies problem by putting a bunch of pea shooting plants in your garden / backyard. Commented Feb 3, 2014 at 15:00
  • 1
    Related: Do zombies exist...in.NET? This has some excellent information about Zombie threads in .NET, what causes them, and how to avoid them. It does not mention zombie objects, though. Are you sure that's what the interview question said? Commented Feb 3, 2014 at 15:17

1 Answer 1

4

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).

Sign up to request clarification or add additional context in comments.

1 Comment

And if your class is a WPF Control, you can use the weak event manager to help with this scenario.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.