C # Asynchronous TCP and garbage collection of its closure instance?

It is simply interesting in the case of asynchronous TCP or another EAP template, if the success handler has a link this, for example. this.state, theoretically, there is a link to the current instance, since it thisis stored in some generated region of the object according to the closure principle. Thus, the instance itself should not be garbage collected, even the area in which the instance is created ends?

My code is similar to the 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)
            {
                // ...
                // ...
            }
        }
}

the code that executes it might look like this:

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

Will there be a GC instance that will result in a Receive () error in general?

+3
source share
1 answer

It depends on how smart the compiler is.

, this , .

, , , 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)
        {
            // ...
            // ...
        }
    }

, :

    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)
        {
            // ...
            // ...
        }
    }

: ? , this, state, state.Socket ? , ( , ).

, BeginReceive/EndReceive,

, (Socket.BeginReceive) System.Net.Sockets.BaseOverlappedAsyncResult.PinUnmanagedObjects.

+2
source

All Articles