diff options
Diffstat (limited to 'src/csharp/Grpc.Core/Internal/RequestCallContextSafeHandle.cs')
-rw-r--r-- | src/csharp/Grpc.Core/Internal/RequestCallContextSafeHandle.cs | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/src/csharp/Grpc.Core/Internal/RequestCallContextSafeHandle.cs b/src/csharp/Grpc.Core/Internal/RequestCallContextSafeHandle.cs index 59e9d9b1ab..ebc2d6d8d6 100644 --- a/src/csharp/Grpc.Core/Internal/RequestCallContextSafeHandle.cs +++ b/src/csharp/Grpc.Core/Internal/RequestCallContextSafeHandle.cs @@ -20,26 +20,26 @@ using System; using System.Runtime.InteropServices; using Grpc.Core; using Grpc.Core.Logging; +using Grpc.Core.Utils; namespace Grpc.Core.Internal { /// <summary> /// grpcsharp_request_call_context /// </summary> - internal class RequestCallContextSafeHandle : SafeHandleZeroIsInvalid, IOpCompletionCallback + internal class RequestCallContextSafeHandle : SafeHandleZeroIsInvalid, IOpCompletionCallback, IPooledObject<RequestCallContextSafeHandle> { static readonly NativeMethods Native = NativeMethods.Get(); static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<RequestCallContextSafeHandle>(); - IObjectPool<RequestCallContextSafeHandle> ownedByPool; + Action<RequestCallContextSafeHandle> returnToPoolAction; private RequestCallContextSafeHandle() { } - public static RequestCallContextSafeHandle Create(IObjectPool<RequestCallContextSafeHandle> ownedByPool = null) + public static RequestCallContextSafeHandle Create() { var ctx = Native.grpcsharp_request_call_context_create(); - ctx.ownedByPool = ownedByPool; return ctx; } @@ -51,6 +51,12 @@ namespace Grpc.Core.Internal } } + public void SetReturnToPoolAction(Action<RequestCallContextSafeHandle> returnAction) + { + GrpcPreconditions.CheckState(returnToPoolAction == null); + returnToPoolAction = returnAction; + } + public RequestCallCompletionDelegate CompletionCallback { get; set; } // Gets data of server_rpc_new completion. @@ -76,10 +82,15 @@ namespace Grpc.Core.Internal public void Recycle() { - if (ownedByPool != null) + if (returnToPoolAction != null) { Native.grpcsharp_request_call_context_reset(this); - ownedByPool.Return(this); + + var origReturnAction = returnToPoolAction; + // Not clearing all the references to the pool could prevent garbage collection of the pool object + // and thus cause memory leaks. + returnToPoolAction = null; + origReturnAction(this); } else { |