somdoron / AsyncIO

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Memory leak in AsyncIO.Windows.Socket

romanros opened this issue · comments

Memory buffer is allocated when calling getter on properties RemoteEndPoint and LocalEndPoint and this memory is not freed. See the constructor of SocketAddress line 29:

      m_bufferHandle = GCHandle.Alloc(m_buffer, GCHandleType.Pinned);

The buffer is freed by Dispose() (SocketAddress.cs). Possible fix below:

        public override IPEndPoint RemoteEndPoint
        {
            get
            {
                using ( var socketAddress = new SocketAddress( AddressFamily, AddressFamily == AddressFamily.InterNetwork ? 16 : 28))
                {
                    int size = socketAddress.Size;

                    if (UnsafeMethods.getpeername(Handle, socketAddress.Buffer, ref size) != SocketError.Success)
                    {
                        throw new SocketException();
                    }

                    return socketAddress.GetEndPoint();
                }
            }
        }

        public override IPEndPoint LocalEndPoint
        {
            get
            {
                using (var  socketAddress = new SocketAddress(AddressFamily, AddressFamily == AddressFamily.InterNetwork ? 16 : 28))
                { 
                    int size = socketAddress.Size;

                    if (UnsafeMethods.getsockname(Handle, socketAddress.Buffer, ref size) != SocketError.Success)
                    {
                        throw new SocketException();
                    }
                    else
                    {
                        return socketAddress.GetEndPoint();
                    }
                }
            }
        }

thanks! do you want to provide a pull request?

I created the pull request. Not sure if I did it right - it's the first time.

This is fixed and can be closed?

yes, thanks. However this is not yet published to nuget. Will try to do it tomorrow

@somdoron has this been published to nuget? If so, perhaps we can close this ticket.