Repository: dotnet/runtime
File: src/libraries/System.Net.Security/src/System/Net/Security/Pal.Android/SafeDeleteSslContext.cs (C#)

Code before:
                int toRead = *dataLength;
                if (toRead == 0)
                    return PAL_SSLStreamStatus.OK;

                if (context._inputBuffer.ActiveLength == 0)
                {
                    *dataLength = 0;
                    return PAL_SSLStreamStatus.NeedData;
                }

                toRead = Math.Min(toRead, context._inputBuffer.ActiveLength);

                context._inputBuffer.ActiveSpan.Slice(0, toRead).CopyTo(new Span<byte>(data, toRead));
                context._inputBuffer.Discard(toRead);

                *dataLength = toRead;
                return PAL_SSLStreamStatus.OK;
            }
        }

        [UnmanagedCallersOnly]
        private static void CleanupManagedContext(IntPtr managedContextHandle)
        {
            if (managedContextHandle != IntPtr.Zero)
            {
                GCHandle handle = GCHandle.FromIntPtr(managedContextHandle);
                handle.Free();
            }
        }

        internal void Write(ReadOnlySpan<byte> buf)
        {
            lock (_lock)
            {
                _inputBuffer.EnsureAvailableSpace(buf.Length);
                buf.CopyTo(_inputBuffer.AvailableSpan);
                _inputBuffer.Commit(buf.Length);
            }
        }

        internal int BytesReadyForConnection => _outputBuffer.ActiveLength;

        internal void ReadPendingWrites(ref ProtocolToken token)
        {
            lock (_lock)
            {
                if (_outputBuffer.ActiveLength == 0)
                {
                    token.Size = 0;
                    token.Payload = null;
                    return;

Review comment (suggestion):
```suggestion
                WeakGCHandle<SafeDeleteSslContext> handle = WeakGCHandle<SafeDeleteSslContext>.FromIntPtr(managedContextHandle);
```

Code after:
                int toRead = *dataLength;
                if (toRead == 0)
                    return PAL_SSLStreamStatus.OK;

                if (context._inputBuffer.ActiveLength == 0)
                {
                    *dataLength = 0;
                    return PAL_SSLStreamStatus.NeedData;
                }

                toRead = Math.Min(toRead, context._inputBuffer.ActiveLength);

                context._inputBuffer.ActiveSpan.Slice(0, toRead).CopyTo(new Span<byte>(data, toRead));
                context._inputBuffer.Discard(toRead);

                *dataLength = toRead;
                return PAL_SSLStreamStatus.OK;
            }
        }

        [UnmanagedCallersOnly]
        private static void CleanupManagedContext(IntPtr managedContextHandle)
        {
            if (managedContextHandle != IntPtr.Zero)
            {
                WeakGCHandle<SafeDeleteSslContext> handle = WeakGCHandle<SafeDeleteSslContext>.FromIntPtr(managedContextHandle);
                handle.Dispose();
            }
        }

        internal void Write(ReadOnlySpan<byte> buf)
        {
            lock (_lock)
            {
                _inputBuffer.EnsureAvailableSpace(buf.Length);
                buf.CopyTo(_inputBuffer.AvailableSpan);
                _inputBuffer.Commit(buf.Length);
            }
        }

        internal int BytesReadyForConnection => _outputBuffer.ActiveLength;

        internal void ReadPendingWrites(ref ProtocolToken token)
        {
            lock (_lock)
            {
                if (_outputBuffer.ActiveLength == 0)
                {
                    token.Size = 0;
                    token.Payload = null;
                    return;