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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:11:13+00:00 2026-05-12T00:11:13+00:00

If my software has two object instances, one of which is subscribed to the

  • 0

If my software has two object instances, one of which is subscribed to the events of the other. Do I need to unsubscribe them from one another before they are orphaned for them to be cleaned up by the garbage collector? Or is there any other reason why I should clear the event relationships? What if the subscribed to object is orphaned but the subscriber is not, or vise versa?

  • 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-12T00:11:14+00:00Added an answer on May 12, 2026 at 12:11 am

    Yes you do. The event publishers are holding references to the objects, and would prevent them from being garbage collected.

    Let’s look at an example to see what happens. We have two classes; one exposes an event, the other consumes it:

    class ClassA
    {
        public event EventHandler Test;
        ~ClassA()
        {
            Console.WriteLine("A being collected");
        }
    }
    class ClassB
    {
        public ClassB(ClassA instance)
        {
            instance.Test += new EventHandler(instance_Test);
        }
    
        ~ClassB()
        {
            Console.WriteLine("B being collected");
        }
    
        void instance_Test(object sender, EventArgs e)
        {
            // this space is intentionally left blank
        }
    }
    

    Note how ClassB does not store a reference to the ClassA instance; it merely hooks up an event handler.

    Now, let’s see how the objects are collected. Scenario 1:

    ClassB temp = new ClassB(new ClassA());
    Console.WriteLine("Collect 1");
    GC.Collect();
    Console.ReadKey();
    temp = null;
    Console.WriteLine("Collect 2");
    GC.Collect();
    Console.ReadKey();
    

    We create a ClassB instance and hold a reference to it through the temp variable. It gets passed a new instance of ClassA, where we do not store a reference to it anywhere, so it goes out of scope immediately after the ClassB constructor is done. We have the garbage collector run once when ClassA has gone out of scope, and once when ClassB as gone out of scope. The output:

    Collect 1
    A being collected
    Collect 2
    B being collected
    

    Scenario 2:

    ClassA temp = new ClassA();
    ClassB temp2 = new ClassB(temp);
    temp2 = null;
    Console.WriteLine("Collect 1");
    GC.Collect();
    Console.ReadKey();
    temp = null;
    Console.WriteLine("Collect 2");
    GC.Collect();
    Console.ReadKey();
    

    A new instance of ClassA is created and a reference to it is stored in the temp variable. Then a new instance of ClassB is created, getting the ClassA instance in temp passed to it, and we store a reference to it in temp2. Then we set temp2 to null, making the ClassB instance going out of scope. As before, we have the garbage collector run after each instance has gone out of scope. The output:

    Collect 1
    Collect 2
    B being collected
    A being collected
    

    So, to conclude; if the instance that exposes an event goes out of scope, it becomes available for garbage collection, regardless of whether there are event handlers hooked up or not. If an instance that has an event handler hooked up to an event in another instance, it will not be available for garbage collection until either the event handler is detached, or the instance to which the event handler is attached becomes available for garbage collection.

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

Sidebar

Related Questions

I am developing a part of a software which has two modules(mail and web-service).But
The software my company uses has a web service which signature is more or
Let's say I have a two wheeled object, where each wheel has an independent
The software produced by my company has several layers and is broken into two
I'm writing an application which has two classes that provide basically the same functionality
We are accessing a .NET object from matlab. It's some image capture software, and
i have two lists which contains guids: var activeSoftware = channels.ByPath(/software/).Children.Where(c => c.StateProperties.IsActive).Select(c =>
I've got two questions really. The first is more a description of events: software
In a software system a one-to-one relationship must be modeled between two objects where
I'm writing some software that targets two versions of very similar hardware which, until

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.