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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:52:31+00:00 2026-05-13T08:52:31+00:00

Lets says we have 2 objects, Broadcaster and Listener. Broadcaster has an event called

  • 0

Lets says we have 2 objects, Broadcaster and Listener. Broadcaster has an event called Broadcast to which Listener is subscribed. If Listener is disposed without unsubscribing from the Broadcast event it will be retained in memory because of the event delegate referencing it that Broadcaster contains.

What I’m curious about is if Broadcaster is disposed without Listener unsubscribing or Broadcaster setting Broadcast = null will Broadcaster be retained in memory?

I have not been able to locate anything with a hard answer to this question except one blogger who believes that not setting the event to null will keep the source in memory (found here).

I’d like to hear an explanation of why or why not.

Thanks.

UPDATE:
Forum Thread where one developer indicates events should be set to null, but Jon Skeet indicates it’s not necessary, but doesn’t elaborate.

  • 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-13T08:52:31+00:00Added an answer on May 13, 2026 at 8:52 am

    Note that delegates don’t keep the publisher alive (they only keep the target=subscriber alive), so no number of subscriptions will (by themselves) keep the broadcaster alive. As such, from this perspective it doesn’t matter whether it is disposed or not. When there are no items referencing the broadcaster (and the event-subscriptions don’t matter for this), it will be eligible for collection.

    Essentially, a delegate is a (list of) pair(s) of MethodInfo and object references; the method to call, and the object to invoke as “arg0” (aka this). It simply doesn’t have a reference to the object raising the event.

    Here’s evidence that a listener does not keep the source alive; you should see that “Source 1” gets collected, even though we still have the matching listener that is subscribed. As expected, “Listener 2” does not get collected, since we still have the matching broadcaster:

    class DataSource
    {
        public DataSource(string name) { this.name = name; }
        private readonly string name;
        ~DataSource() { Console.WriteLine("Collected: " + name); }
    
        public event EventHandler SomeEvent;
    }
    class DataListener
    {
        public DataListener(string name) { this.name = name; }
        private readonly string name;
        ~DataListener() { Console.WriteLine("Collected: " + name); }
        public void Subscribe(DataSource source)
        {
            source.SomeEvent += SomeMethodOnThisObject;
        }
        private void SomeMethodOnThisObject(object sender, EventArgs args) { }
    }
    
    static class Program
    {
        static void Main()
        {
            DataSource source1 = new DataSource("Source 1"),
                    source2 = new DataSource("Source 2");
            DataListener listener1 = new DataListener("Listener 1"),
                    listener2 = new DataListener("Listener 2");
            listener1.Subscribe(source1);
            listener2.Subscribe(source2);
            // now we'll release one source and one listener, and force a collect
            source1 = null;
            listener2 = null;
            GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
            GC.WaitForPendingFinalizers(); // source 1 gets collected, ONLY
    
            Console.WriteLine("Done");
            Console.ReadLine();
            GC.KeepAlive(source2); // prevents collection due to optimisation
            GC.KeepAlive(listener1); // prevents collection due to optimisation
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say at your job your boss says, That system over there, which has
Lets say I have a Dictionary object: Dictionary myDictionary<int, SomeObject> = new Dictionary<string, SomeObject>();
Lets say that you have websites www.xyz.com and www.abc.com. Lets say that a user
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
Lets say I have several web sites on my web server, all as applications
Lets assume we have this xml: <?xml version=1.0 encoding=UTF-8?> <tns:RegistryResponse status=urn:oasis:names:tc:ebxml-regrep:ResponseStatusType:Failure xmlns:tns=urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0 xmlns:rim=urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0> <tns:RegistryErrorList
Lets say I have an array like this: string [] Filelist = ... I
Lets assume my classes are loaded from a compressed .war file or loaded elsewhere,
Lets say on MySQL database (if it matters).
Lets say I've got two interchangeable pieces of code and I want to figure

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.