mmdeploy/csrc/apis/csharp/MMDeploy/APIs/DisposableObject.cs
Chen Xin 9e6a3c8ec5
add csharp support (#388)
* add csharp support, init commit

* export MMDeploySharpExtern.dll when build sdk

* refactor code

* multi frameworks

* move csharp demo to demo folder

* try to fix lint

* try to fix lint

* update csharp demo Readme

* rename MMDeploySharp -> MMDeploy

* add comment why build MMDeployExtern.dll

* squeeze MMDeploy project

* remove Mm

* print error code

* update c# api build README.md

* fix exception

* fix exception

* update demo

* update README.md

* fix typo

* fix ci

* fix ci

* fix formatresult

* add options whether build MMDeployExtern.dll

* update CMakeListst.txt

* change MMDEPLOY_BUILD_CSHARP_EXTERN -> MMDEPLOY_BUILD_SDK_CSHARP_API

* c# api -> C# API

Co-authored-by: chenxin2 <chenxin2@sensetime.com>
2022-05-27 16:02:29 +08:00

97 lines
2.2 KiB
C#

using System;
using System.Runtime.InteropServices;
namespace MMDeploy
{
/// <summary>
/// Base class which manages its own memory.
/// </summary>
public class DisposableObject : IDisposable
{
#pragma warning disable SA1401 // Fields should be private
/// <summary>
/// Handle pointer.
/// </summary>
protected IntPtr _handle;
#pragma warning restore SA1401 // Fields should be private
private bool _disposed = false;
/// <summary>
/// Gets a value indicating whether this instance has been disposed.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Close handle.
/// </summary>
public void Close()
{
Dispose();
}
/// <summary>
/// Releases the resources.
/// </summary>
private void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
if (disposing)
{
// Free any other managed objects here.
ReleaseManaged();
}
// Free any unmanaged objects here.
ReleaseHandle();
_handle = IntPtr.Zero;
_disposed = true;
}
/// <summary>
/// Releases managed resources.
/// </summary>
protected virtual void ReleaseManaged()
{
}
/// <summary>
/// Releases unmanaged resources.
/// </summary>
protected virtual void ReleaseHandle()
{
Marshal.FreeHGlobal(_handle);
}
/// <summary>
/// Finalizes an instance of the <see cref="DisposableObject"/> class.
/// </summary>
~DisposableObject()
{
Dispose(false);
}
/// <summary>
/// Throw exception is result is not zero.
/// </summary>
/// <param name="result">function return value.</param>
protected static void ThrowException(int result)
{
if (result != 0)
{
throw new Exception(result.ToString());
}
}
}
}