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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:52:54+00:00 2026-05-11T03:52:54+00:00

I have a class that offers up a few events. That class is declared

  • 0

I have a class that offers up a few events. That class is declared globally but not instanced upon that global declaration–it’s instanced on an as-needed basis in the methods that need it.

Each time that class is needed in a method, it is instanced and event handlers are registered. Is it necessary to remove the event handlers explicitly before the method goes out of scope?

When the method goes out of scope, so goes the instance of the class. Does leaving event handlers registered with that instance that is going out of scope have a memory footprint implication? (I’m wondering if the event handler keeps the GC from seeing the class instance as no longer being referenced.)

  • 1 1 Answer
  • 3 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. 2026-05-11T03:52:54+00:00Added an answer on May 11, 2026 at 3:52 am

    In your case, everything is fine. It’s the object which publishes the events which keeps the targets of the event handlers live. So if I have:

    publisher.SomeEvent += target.DoSomething; 

    then publisher has a reference to target but not the other way round.

    In your case, the publisher is going to be eligible for garbage collection (assuming there are no other references to it) so the fact that it’s got a reference to the event handler targets is irrelevant.

    The tricky case is when the publisher is long-lived but the subscribers don’t want to be – in that case you need to unsubscribe the handlers. For example, suppose you have some data transfer service which lets you subscribe to asynchronous notifications about bandwidth changes, and the transfer service object is long-lived. If we do this:

    BandwidthUI ui = new BandwidthUI(); transferService.BandwidthChanged += ui.HandleBandwidthChange; // Suppose this blocks until the transfer is complete transferService.Transfer(source, destination); // We now have to unsusbcribe from the event transferService.BandwidthChanged -= ui.HandleBandwidthChange; 

    (You’d actually want to use a finally block to make sure you don’t leak the event handler.) If we didn’t unsubscribe, then the BandwidthUI would live at least as long as the transfer service.

    Personally I rarely come across this – usually if I subscribe to an event, the target of that event lives at least as long as the publisher – a form will last as long as the button which is on it, for example. It’s worth knowing about this potential issue, but I think some people worry about it when they needn’t, because they don’t know which way round the references go.

    EDIT: This is to answer Jonathan Dickinson’s comment. Firstly, look at the docs for Delegate.Equals(object) which clearly give the equality behaviour.

    Secondly, here’s a short but complete program to show unsubscription working:

    using System;  public class Publisher {     public event EventHandler Foo;      public void RaiseFoo()     {         Console.WriteLine('Raising Foo');         EventHandler handler = Foo;         if (handler != null)         {             handler(this, EventArgs.Empty);         }         else         {             Console.WriteLine('No handlers');         }     } }  public class Subscriber {     public void FooHandler(object sender, EventArgs e)     {         Console.WriteLine('Subscriber.FooHandler()');     } }  public class Test {     static void Main()     {          Publisher publisher = new Publisher();          Subscriber subscriber = new Subscriber();          publisher.Foo += subscriber.FooHandler;          publisher.RaiseFoo();          publisher.Foo -= subscriber.FooHandler;          publisher.RaiseFoo();     } } 

    Results:

    Raising Foo Subscriber.FooHandler() Raising Foo No handlers 

    (Tested on Mono and .NET 3.5SP1.)

    Further edit:

    This is to prove that an event publisher can be collected while there are still references to a subscriber.

    using System;  public class Publisher {     ~Publisher()     {         Console.WriteLine('~Publisher');         Console.WriteLine('Foo==null ? {0}', Foo == null);     }      public event EventHandler Foo; }  public class Subscriber {     ~Subscriber()     {         Console.WriteLine('~Subscriber');     }      public void FooHandler(object sender, EventArgs e) {} }  public class Test {     static void Main()     {          Publisher publisher = new Publisher();          Subscriber subscriber = new Subscriber();          publisher.Foo += subscriber.FooHandler;           Console.WriteLine('No more refs to publisher, '              + 'but subscriber is alive');          GC.Collect();          GC.WaitForPendingFinalizers();                    Console.WriteLine('End of Main method. Subscriber is about to '              + 'become eligible for collection');          GC.KeepAlive(subscriber);     } } 

    Results (in .NET 3.5SP1; Mono appears to behave slightly oddly here. Will look into that some time):

    No more refs to publisher, but subscriber is alive ~Publisher Foo==null ? False End of Main method. Subscriber is about to become eligible for collection ~Subscriber 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 86k
  • Answers 86k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use stringstream::str() method which returns the associated std::string… May 11, 2026 at 5:18 pm
  • Editorial Team
    Editorial Team added an answer If I remember correctly, setting display: inline to this DIV… May 11, 2026 at 5:18 pm
  • Editorial Team
    Editorial Team added an answer You haven't told us how the fields in table3 are… May 11, 2026 at 5:18 pm

Related Questions

I am in the process of beginning work on several ASP.NET custom controls. I
Okay, this one is REALLY weird. I'm using .net for my backend and Flex
I have a method that contains an asynchronous call like this: public void MyMethod()
According to the official (gregorian) calendar , the week number for 29/12/2008 is 1,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.