Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using SafeHandle in Context class #8072

Merged
merged 2 commits into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 54 additions & 12 deletions wrappers/csharp/Intel.RealSense/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,37 @@ namespace Intel.RealSense
{
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.Runtime.ConstrainedExecution;
using System.Security.Permissions;


[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
public sealed class ContextHandle : SafeHandleZeroOrMinusOneIsInvalid
{
public ContextHandle()
: base(true)
{
}

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
override protected bool ReleaseHandle()
{
if (!IsClosed)
{
NativeMethods.rs2_delete_context(this);
}
return true;
}
}

/// <summary>
/// default librealsense context class
/// </summary>
public class Context : Base.Object
public class Context : IDisposable
{
private ContextHandle handle;

static Context()
{
object error;
Expand All @@ -21,11 +46,29 @@ static Context()
/// Initializes a new instance of the <see cref="Context"/> class.
/// </summary>
public Context()
: base(Create(), NativeMethods.rs2_delete_context)
{
object error;
handle = NativeMethods.rs2_create_context(ApiVersion, out error);
onDevicesChangedCallback = new rs2_devices_changed_callback(OnDevicesChangedInternal);
}

/// <summary>
/// Gets the safe handle
/// </summary>
/// <exception cref="ObjectDisposedException">Thrown when <see cref="SafeHandle.IsInvalid"/></exception>
public ContextHandle Handle
{
get
{
if (handle.IsInvalid)
{
throw new ObjectDisposedException(GetType().Name);
}

return handle;
}
}

/// <summary>
/// the version API encoded into integer value "1.9.3" -> 10903
/// </summary>
Expand All @@ -47,12 +90,6 @@ public string Version
}
}

internal static IntPtr Create()
{
object error;
return NativeMethods.rs2_create_context(ApiVersion, out error);
}

// Keeps the delegate alive, if we were to assign onDevicesChanged directly, there'll be
// no managed reference it, it will be collected and cause a native exception.
private readonly rs2_devices_changed_callback onDevicesChangedCallback;
Expand Down Expand Up @@ -80,7 +117,7 @@ public event OnDevicesChangedDelegate OnDevicesChanged
if (OnDevicesChangedEvent == null)
{
object error;
NativeMethods.rs2_set_devices_changed_callback(Handle, onDevicesChangedCallback, IntPtr.Zero, out error);
NativeMethods.rs2_set_devices_changed_callback(handle, onDevicesChangedCallback, IntPtr.Zero, out error);
}

OnDevicesChangedEvent += value;
Expand All @@ -105,7 +142,7 @@ public event OnDevicesChangedDelegate OnDevicesChanged
public DeviceList QueryDevices(bool include_platform_camera = false)
{
object error;
var ptr = NativeMethods.rs2_query_devices_ex(Handle, include_platform_camera ? 0xff : 0xfe, out error);
var ptr = NativeMethods.rs2_query_devices_ex(handle, include_platform_camera ? 0xff : 0xfe, out error);
return new DeviceList(ptr);
}

Expand All @@ -127,7 +164,7 @@ public DeviceList Devices
public PlaybackDevice AddDevice(string file)
{
object error;
var ptr = NativeMethods.rs2_context_add_device(Handle, file, out error);
var ptr = NativeMethods.rs2_context_add_device(handle, file, out error);
return Device.Create<PlaybackDevice>(ptr);
}

Expand All @@ -136,7 +173,7 @@ public PlaybackDevice AddDevice(string file)
public void RemoveDevice(string file)
{
object error;
NativeMethods.rs2_context_remove_device(Handle, file, out error);
NativeMethods.rs2_context_remove_device(handle, file, out error);
}

private void OnDevicesChangedInternal(IntPtr removedList, IntPtr addedList, IntPtr userData)
Expand All @@ -151,5 +188,10 @@ private void OnDevicesChangedInternal(IntPtr removedList, IntPtr addedList, IntP
}
}
}

public void Dispose()
{
handle.Dispose();
}
}
}
Loading