Skip to content

Commit e87cc04

Browse files
sharwellEsther2013
authored andcommitted
Optimize SafeHandleExtensions.Lease()
Implements changes suggested in dotnet/roslyn#43462.
1 parent d264a80 commit e87cc04

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

src/TensorFlowNET.Core/Util/SafeHandleExtensions.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,22 @@ limitations under the License.
1515
******************************************************************************/
1616

1717
using System;
18+
using System.Diagnostics;
1819
using System.Runtime.InteropServices;
1920

2021
namespace Tensorflow.Util
2122
{
2223
internal static class SafeHandleExtensions
2324
{
2425
/// <summary>
25-
/// Acquires a lease on a safe handle. This method is intended to be used in the initializer of a <c>using</c>
26-
/// statement.
26+
/// Acquires a lease on a safe handle. The lease increments the reference count of the <see cref="SafeHandle"/>
27+
/// to ensure the handle is not released prior to the lease being released.
2728
/// </summary>
29+
/// <remarks>
30+
/// This method is intended to be used in the initializer of a <c>using</c> statement. Failing to release the
31+
/// lease will permanently prevent the underlying <see cref="SafeHandle"/> from being released by the garbage
32+
/// collector.
33+
/// </remarks>
2834
/// <param name="handle">The <see cref="SafeHandle"/> to lease.</param>
2935
/// <returns>A <see cref="SafeHandleLease"/>, which must be disposed to release the resource.</returns>
3036
/// <exception cref="ObjectDisposedException">If the lease could not be acquired.</exception>
@@ -37,8 +43,7 @@ public static SafeHandleLease Lease(this SafeHandle handle)
3743
try
3844
{
3945
handle.DangerousAddRef(ref success);
40-
if (!success)
41-
throw new ObjectDisposedException(handle.GetType().FullName);
46+
Debug.Assert(success, $"'{nameof(SafeHandle.DangerousAddRef)}' does not return when '{nameof(success)}' is false.");
4247

4348
return new SafeHandleLease(handle);
4449
}

0 commit comments

Comments
 (0)