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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:13:15+00:00 2026-05-27T12:13:15+00:00

class A { public event EventHandler AEvent; } class B { private A _foo;

  • 0
class A
{
   public event EventHandler AEvent;
}
class B
{
   private A _foo;
   private int _bar;

   public void AttachToAEvent()
   {
      _foo.AEvent += delegate()
      {
         ...
         UseBar(_bar);
         ...
      }
   }
} 

Since delegate captures variable this._bar, does it implicitly hold to the instance of B? Will instance of B be referenced through the event handler and captured variable by an instance of A?

Would it be different if _bar was a local variable of the AttachToAEvent method?

Since in my case an instance of A lives far longer and is far smaller than an instance of B, I’m worried to cause a “memory leak” by doing this.

  • 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-27T12:13:16+00:00Added an answer on May 27, 2026 at 12:13 pm

    This is easiest understood by looking at the code generated by the compiler, which is similar to:

    public void AttachToAEvent()
    {
        _foo.AEvent += new EventHandler(this.Handler);
    }
    
    [CompilerGenerated]
    private void Handler(object sender, EventArgs e)
    {
        this.UseBar(this._bar);
    }
    

    As can be plainly seen, the delegate created is an instance-delegate (targets an instance method on an object) and must therefore hold a reference to this object instance.

    Since delegate captures variable this._bar, does it implicitly hold to
    the instance of B?

    Actually, the anonymous method captures just this (not this._bar). As can be seen from the generated code, the constructed delegate will indeed hold a reference to the B instance. It has to; how else could the field be read on demand whenever the delegate is executed? Remember that variables are captured, not values.

    Since in my case an instance of A lives far longer and is far smaller
    than an instance of B, I’m worried to cause “memory leak” by doing
    this.

    Yes, you have every reason to be. As long as the A instance is reachable, the B event-subscriber will still be reachable. If you don’t want to go fancy with weak-events, you need to rewrite this so the handler is unregistered when it is no longer required.

    Would it be different if _bar was a local variable of the
    AttachToAEvent method?

    Yes, it would, as the captured variable would then become the bar local rather than this.
    But assuming that UseBar is an instance-method, your “problem” (if you want tot think of it that way) has just gotten worse. The compiler now needs to generate an event-listener that “remembers” both the local and the containing B object instance.

    This is accomplished by creating a closure object and making it (really an instance method of it) the target of the delegate.

    public void AttachToAEvent(int _bar)
    {
        Closure closure = new Closure();
        closure._bar = _bar;
        closure._bInstance = this;
        _foo.AEvent += new EventHandler(closure.Handler);
    }
    
    [CompilerGenerated]
    private sealed class Closure
    {
        public int _bar;
        public B _bInstance;
    
        public void Handler(object sender , EventArgs e)
        {
            _bInstance.UseBar(this._bar);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public sealed class FtpManager { public event EventHandler LoggingIn = delegate { }; private
My class with an event: public class WindowModel { public delegate void WindowChangedHandler(object source,
colLabels[i].addMouseListener(new MyAdapter()); private class MyAdapter extends MouseAdapter { @Override public void mouseClicked(MouseEvent event) {
public class a { public event eventhandler test; public void RaiseTest(){//fire test} } Is
Why would a 'public event EventHandler cccc' be null? I have a class that's
Let's say we have the following setup: public class ClassA { public event EventHandler
i have the following: a game class class Game { public event EventHandler GameOver;
My class has an event which external objects will subscribe to: public event EventHandler<MessageReceivedEventArgs>
Suppose I have public class Parent { private IList<Child> _children; public void AddChild(Child child)
Imagine the following class: class A { public event EventHandler AnyEvent; } You create

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.