internal void FreeNativeOverlapped()
{
// Do not call free during AppDomain shutdown, there may be an outstanding operation.
// Overlapped will take care calling free when the native callback completes.
IntPtr oldHandle = Interlocked.Exchange(ref handle, IntPtr.Zero);
if (oldHandle != IntPtr.Zero && !Environment.HasShutdownStarted)
{
unsafe
{
Debug.Assert(SocketHandle != null, "SocketHandle is null.");
ThreadPoolBoundHandle boundHandle = SocketHandle.IOCPBoundHandle;
Debug.Assert(boundHandle != null, "SafeNativeOverlapped::FreeNativeOverlapped - boundHandle is null");
// FreeNativeOverlapped will be called even if boundHandle was previously disposed.
boundHandle?.FreeNativeOverlapped((NativeOverlapped*)oldHandle);
}
}
}
}