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

  • SEARCH
  • Home
  • 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 8448529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:27:09+00:00 2026-06-10T10:27:09+00:00

When does these unsubscribed events memory leak occurs? Should I write destructor or implement

  • 0

When does these unsubscribed events memory leak occurs? Should I write destructor or implement IDisposable to unsubscribe an event?

  • 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-06-10T10:27:10+00:00Added an answer on June 10, 2026 at 10:27 am

    Let’s say that A references B. Furthermore, say you think you’re done with B and expect it to be garbage collected.

    Now, if A is reachable[1], B won’t be garbage collected, despite the fact that “you’re done with it”. This is, in all essence, a memory leak[2]

    If B subscribes to an event in A, then we have the same situation: A has a reference to B via the event handler delegate.

    So, when is this a problem? Only when the referencing object is reachable, as mentioned above. In this case, there can be a leak when a Foo instance isn’t used any longer:

    class Foo
    {
        Bar _bar;
    
        public Foo(Bar bar)
        {
            _bar = bar;
            _bar.Changed += BarChanged;
        }
    
        void BarChanged(object sender, EventArgs e) { }
    }
    

    The reason why there can be a leak is that the Bar instance passed in the constructor can have a longer lifetime than the Foo instance using it. The subscribed event handler can then keep the Foo alive.

    In this case you need to provide a way to unsubscribe from the event to not get a memory leak. One way of doing that is by letting Foo implement IDisposable. The upside of that is that it clearly signals to the class consumer that he need to call Dispose() when done. Another way is to have separate Subscribe() and Unsubscribe() methods, but that doesn’t convey the type’s expectations – they are too optional to call and introduce a temporal coupling.

    My recommendation is:

    class sealed Foo : IDisposable
    {
        readonly Bar _bar;
        bool _disposed;
    
        ...
    
        public void Dispose()
        {
            if (!_disposed)
            {
                _disposed = true;
                _bar.Changed -= BarChanged;
            }
        }
    
        ...
    }
    

    Or alternatively:

    class sealed Foo : IDisposable
    {
        Bar _bar;
    
        ...
    
        public void Dispose()
        {
            if (_bar != null)
            {
                _bar.Changed -= BarChanged;
                _bar = null;
            }
        }
    
        ...
    }
    

    On the other hand, when the referencing object isn’t reachable, there can’t be a leak:

    class sealed Foo
    {
        Bar _bar;
    
        public Foo()
        {
            _bar = new Bar();
            _bar.Changed += BarChanged;
        }
    
        void BarChanged(object sender, EventArgs e) { }
    }
    

    In this case any Foo instance will always outlive its composed Bar instance. When a Foo is unreachable, so will its Bar be. The subscribed event handler cannot keep the Foo alive here. The downside of this is that if Bar is a dependency in need of being mocked in unit testing scenarios, it can’t (in any clean way) be explicitly instantiated by the consumer, but needs to be injected.

    [1] http://msdn.microsoft.com/en-us/magazine/bb985010.aspx

    [2] http://en.wikipedia.org/wiki/Memory_leak

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

Sidebar

Related Questions

Does any of these logging libraries support memory mapped files? If not, is there
Does this code create a memory leak? WebClient client = new WebClient(); client.DownloadDataCompleted +=
Possible Duplicate: does these code has memory leakage?? static private ArrayList seriesColors = new
All are from this post . What does these statement mean: error(nargchk(5, 6, nargin));
I'm hoping someone here can answer these definitively: Does putting a VHD file in
Does anyone know what these error's mean? I get this when I click a
Does anybody know the difference between these two commands to switch and track a
Question: Where does productArray come from in these: for ( Product product : productArray
Our company does work environment surveys, and these surveys are filled in online. All
What does rspec gem do? It is named Behaviour driven development tool, but these

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.