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