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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:06:30+00:00 2026-05-20T12:06:30+00:00

Just wondering in case of Asynchronous TCP or other EAP pattern, if the success

  • 0

Just wondering in case of Asynchronous TCP or other EAP pattern, if the success handler has a reference of this, e.g. this.state, in theory, there is a reference to current instance, as this is kept in some generated object scope by the closure nature. Thus the instance itself should not be garbage collected even the scope which create the instance is finished executing?

My code is similar to following:

 public class ATcpClient
    {
        private ATcpState state = null;

        private void Receive()
        {
            // create the callback here, in order to use in dynamic
            AsyncCallback ReceiveCallback = delegate(IAsyncResult ar)
                {
                    try
                    {
                        // Read data from the remote device.
                        this.state.BytesReceived = this.state.Socket.EndReceive(ar);

                    }
                    catch (Exception e)
                    {
                        // ...

                    }
                };

            try
            {
                    this.state.Socket.BeginReceive(this.state.Buffer, 0, this.state.BufferSize, 0,
                        ReceiveCallback, null);
            }
            catch (Exception e)
            {
                // ...
                // ...
            }
        }
}

code which execute it can look like:

public void DoExecuteCode()
{
    new ATcpClient().Receive();
}

Would the instance been GC which cause the Receive() to fail as a whole?

  • 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-20T12:06:31+00:00Added an answer on May 20, 2026 at 12:06 pm

    That depends on how clever the compiler is.

    In your case, yes, this will definitely be kept alive by the delegate, for as long as the delegate lives.

    Let’s look at a case that will definitely NOT keep this alive:

        private void Receive()
        {
            ATcpState state = this.state;
            // create the callback here, in order to use in dynamic
            AsyncCallback ReceiveCallback = delegate(IAsyncResult ar)
                {
                    try
                    {
                        // Read data from the remote device.
                        state.BytesReceived = state.Socket.EndReceive(ar);
    
                    }
                    catch (Exception e)
                    {
                        // ...
    
                    }
                };
    
            try
            {
                    state.Socket.BeginReceive(state.Buffer, 0, state.BufferSize, 0,
                        ReceiveCallback, null);
            }
            catch (Exception e)
            {
                // ...
                // ...
            }
        }
    

    And then a case where compiler optimizations could affect the collection behavior:

        private readonly ATcpState state = new ATcpState();
        private void Receive()
        {
            // create the callback here, in order to use in dynamic
            AsyncCallback ReceiveCallback = delegate(IAsyncResult ar)
                {
                    try
                    {
                        // Read data from the remote device.
                        state.BytesReceived = state.Socket.EndReceive(ar);
    
                    }
                    catch (Exception e)
                    {
                        // ...
    
                    }
                };
    
            try
            {
                    state.Socket.BeginReceive(state.Buffer, 0, state.BufferSize, 0,
                        ReceiveCallback, null);
            }
            catch (Exception e)
            {
                // ...
                // ...
            }
        }
    

    The only remaining question is, what is the lifetime of that delegate? Is the pending operation a root, or do we just have a circular reference between the delegate, possibly this, state, state.Socket, and the operation? If none of these are reachable from a root then the whole bunch can be finalized (which would close the socket, cancelling the operation).

    At least for some objects using the BeginReceive/EndReceive pattern, the operation is NOT a root

    It looks like for your case (Socket.BeginReceive), a root is created inside System.Net.Sockets.BaseOverlappedAsyncResult.PinUnmanagedObjects.

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

Sidebar

Related Questions

I'm just wondering if there is a best case to write this code: $('#set_duration_30').click(function(event)
Just wondering: I'm trying to set up an adaptive image handler in Coldfusion8, which
Ok, just wondering on the versions of PHP that this class is built into.
I'm just wondering what happens in the case of a time zone changing, when
I`m just wondering.... If it was the case that I was thinking in creating
I'm pretty new to programming, and I was just wondering in the following case
Just wondering if anyone has any idea of why my filter order is being
Just wondering, Would the SqlConnection be diposed/closed when this method is done? Or do
Just wondering why people like case sensitivity in a programming language? I'm not trying
I'm just wondering if there can be a case where the hostname can be

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.