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 8578213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:20:08+00:00 2026-06-11T20:20:08+00:00

I have been reading up on memory management and have come across a situation

  • 0

I have been reading up on memory management and have come across a situation in a project where the book, nor Google has come up with an exact answer. I already know that delegates are manages objects and events are delegate instances. Having said that, delegate instances will be removed from memory once the application ends.

What I can’t figure out is how to ensure that external code has freed up all event references by the time my class is disposed (either explicitly or by the GC). As an example, class A exposes an event and class B consumes it. Class B calls Dispose on class A without freeing up references to the delegates. Of course, we cannot throw an error from the Dispose method itself.

Following is a class with a delegate and another one that consumes it.

public class ClassB
{
    private ClassA A { get; set; }

    public ClassB()
    {
        this.A = new ClassA();
        this.A.OnProcessed += new ClassA.DelegateProcessed(this.ClassA_Processed);
    }

    public void Process()
    {
        this.A.Process();
    }

    public void ClassA_Processed (ClassA sender, EventArgs e)
    {
        // Do something.

        // Code written by another developer does not free up events before calling Dispose.

        this.A.Dispose();
        this.A = null;
    }
}

public class ClassA: IDisposable
{
    public delegate void DelegateProcessed (A sender, EventArgs e);
    public event DelegateProcessed OnProcessed = null;

    ~ClassA() { this.Dispose(false); }

    public void Dispose ()
    {
        this.Dispose(true);
        System.GC.SuppressFinalize(this);
    }

    private void Dispose (bool disposing)
    {
        if (!this.Disposed)
        {
            if (disposing)
            {
                // Dispose managed resources here.
                // Is it possible / advisable to dispose of delegates / events here?
                // Will this adversely affect the consumer class?
                this.OnProcessed -= new ClassA.DelegateProcessed(this.ClassA_Processed);
            }
        }
        this.Disposed = true;
    }

    public void Process () { this.OnProcessed(this, new EventArgs()); }

    public void ClassA_Processed (ClassA sender, EventArgs e) { }
}

The point is to ensure that ClassA qualifies for garbage collection no matter what the developer does with ClassB. The point is to minimize the amount of time ClassA spends in memory even if the consumer is careless.

UPDATE: It is clear from the answers that the events do not have to be explicitly removed from ClassA. As for the main question, weak references seem to be the way to go as answered below. The objective is to minimize the time ClassA stays in memory. Please let me know in case I have overlooked anythig.

  • 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-11T20:20:09+00:00Added an answer on June 11, 2026 at 8:20 pm

    Instead of the “classic” Event subscriptions, you should have a look at the Weak Event Patterns.

    Event subscriptions may keep objects alive, even if these references are the only references left and the referenced object itself is already gone out of scope. In this case the referenced object will never be collected by the GarbageCollector and stays alive until the end of your application.

    This causes serious memory leaks.

    If you are using the Weak Events pattern you allow the GabageCollector to better determine if the object is still referenced or if the events are the only references. In this case the objects get collected and your resources get freed.

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

Sidebar

Related Questions

A chapter out of the book I have been reading has focused on memory
I have been reading different articles on memory management in preparing for how I
I have been reading about making ajax heavy applications more search engine friendly: https://developers.google.com/webmasters/ajax-crawling/docs/getting-started
I have been reading up to try to make sense of memory leaks in
I have been reading about out of memory conditions on Linux, and the following
I've been reading the Beginning iPhone 4 Development book and they have one tutorial
So I've been reading through the K&R C book and have a question.. in
I have just been getting into low level programming (reading/writing to memory that sort
I am having some memory leak problems in IE. I have been reading, that
I have been lately reading a lot on memory allocation schemes in java, and

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.