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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:49:53+00:00 2026-05-16T23:49:53+00:00

Consider the following code: public class Bar { Foo foo; void Go() { foo

  • 0

Consider the following code:

public class Bar {
    Foo foo;
    void Go() {
       foo = new Foo();
       foo.Send(...);

       foo.Dispose();
       foo = null; 
    }
}

public class Foo : IDisposable {   
    public void Send(byte[] bytes) {
        SocketAsyncEventArgs args = new SocketAsyncEventArgs();
        args.SetBuffer(bytes, 0, bytes.Length);
        args.UserToken = socket;
        args.RemoteEndPoint = endPoint;
        args.Completed += new EventHandler<SocketAsyncEventArgs>(OnSendCompleted);

        socket.SendAsync(args);
    }

    private void OnSendCompleted(object sender, SocketAsyncEventArgs e) {
        Debug.WriteLine("great");
    }

    public void Dispose() {
       //
    }
}

So class Bar runs the Init method which instantiates the Foo class and fires off the Send method, then destroys the Foo instance. The Send method meanwhile instantiates a method level SocketAsyncEventArgs, sets up a Completed event and then fires off the SendAsync method.

Assuming that the SendAsync completes after the Foo instance has been set to null, what happens to the event handler? Does it still fire? If I don’t want it to fire, how do I properly clean up the Foo class, knowing that a method level variable spawned off an event.

  • 1 1 Answer
  • 2 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-16T23:49:54+00:00Added an answer on May 16, 2026 at 11:49 pm

    Yes, it will still fire. Setting a variable to null doesn’t trigger garbage collection or anything like that. It just sets the variable to null. (It’s important to differentiate between a variable and an instance. There’s no such concept as “setting an instance to null”. If I write my home address down on a piece of paper, then rub it out again, that doesn’t destroy my house.)

    It sounds like you may want your Dispose method to “remember” that the object has been cleaned up, and then if OnSendCompleted is called after disposal, just ignore it. Alternatively, keep track of any “requests in flight” and cancel them in in Dispose… noting that some requests might complete while you’re cancelling the whole lot.

    Another point to note: instead of explicitly calling Dispose(), you should almost always use a using statement, which will make sure that Dispose() is called however the using statement ends (e.g. with an exception).

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

Sidebar

Related Questions

Consider the following code: #include <iostream> class Bar { public: void foo(bool b =
Consider the following example code: class Foo { }; class Bar : public Foo
Consider the following code: abstract class Foo<T> where T : Foo<T>, new() { void
Consider the following example class Foo{ public: Foo(int i):x(i){} int x; }; void bar(Foo
Consider the following code: class A { public: void foo() { auto functor =
Consider the following code: class Base { public: virtual void Foo() {} }; class
Consider the following code: class A { public: virtual void f() throw ( int
Consider the following C++ code: class A { public: virtual void f()=0; }; int
Consider the following code. public interface IFoo { } public class Bar { public
Consider the following code :- public class UsingWait1{ public static void main(String... aaa){ CalculateSeries

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.