diff options
author | 2018-01-24 16:16:23 -0800 | |
---|---|---|
committer | 2018-01-24 16:16:23 -0800 | |
commit | fd900e09fbafad16159617af86ca5b56f4819182 (patch) | |
tree | 8cc6e609fcec3e38e6966143f53cb2bd1d2417df /src/csharp/Grpc.Core/Internal/RequestCallContextSafeHandle.cs | |
parent | 401f903151825dc06a90ab145791828276278521 (diff) | |
parent | 7e05426477dbb0235b806ddaa87b6ea95031efdd (diff) |
Merge remote-tracking branch 'upstream/master' into fix-stream-compression-config-interface
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 { |