forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reset a Base.Object deleter handle to an invalid handle upon Dispose(…
…) so a disposed object can't continue to be used if the native handle is still alive. Add a new RefCountedDeleterHandle to differentiate between a non-refcounted DeleterHandle.
- Loading branch information
Showing
4 changed files
with
69 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
target_sources(${LRS_DOTNET_TARGET} | ||
PRIVATE | ||
"${CMAKE_CURRENT_LIST_DIR}/DeleterHandle.cs" | ||
"${CMAKE_CURRENT_LIST_DIR}/RefCountedDeleterHandle.cs" | ||
"${CMAKE_CURRENT_LIST_DIR}/Object.cs" | ||
"${CMAKE_CURRENT_LIST_DIR}/PooledObject.cs" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
wrappers/csharp/Intel.RealSense/Base/RefCountedDeleterHandle.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// License: Apache 2.0. See LICENSE file in root directory. | ||
// Copyright(c) 2017 Intel Corporation. All Rights Reserved. | ||
|
||
namespace Intel.RealSense.Base | ||
{ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
using System.Security; | ||
|
||
|
||
/// <summary> | ||
/// Native handle with deleter delegate to release unmanaged resources | ||
/// The unmanaged resource is released when the refcount reaches 0 | ||
/// </summary> | ||
// TODO: CriticalFinalizerObject & CER | ||
//[DebuggerDisplay("{deleter?.Method.Name,nq}", Name = nameof(Deleter))] | ||
internal class RefCountedDeleterHandle : DeleterHandle | ||
{ | ||
private int refCount; | ||
|
||
public RefCountedDeleterHandle(IntPtr ptr, Deleter deleter) : base(ptr, deleter) | ||
{ | ||
Retain(); | ||
} | ||
|
||
public RefCountedDeleterHandle() : base(IntPtr.Zero, null) | ||
{ | ||
SetHandleAsInvalid(); | ||
} | ||
|
||
public void Retain() | ||
{ | ||
refCount++; | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
if (Handle == IntPtr.Zero) | ||
{ | ||
return; | ||
} | ||
|
||
refCount--; | ||
if (refCount == 0) | ||
{ | ||
base.Dispose(disposing); | ||
} | ||
} | ||
|
||
~RefCountedDeleterHandle() | ||
{ | ||
Dispose(false); | ||
} | ||
} | ||
} |