diff --git a/src/Native/include/nncase/compiler.h b/src/Native/include/nncase/compiler.h index 7339d0b546..1ef12f990d 100644 --- a/src/Native/include/nncase/compiler.h +++ b/src/Native/include/nncase/compiler.h @@ -109,6 +109,7 @@ typedef struct { clr_object_handle_t (*calibration_dataset_provider_create)( clr_object_handle_t dataset, size_t samplesCount, clr_object_handle_t fn_params); + void (*handle_dispose)(clr_object_handle_t handle); void (*handle_free)(clr_object_handle_t handle); clr_object_handle_t (*compile_options_create)(); void (*compile_options_set_input_file)(clr_object_handle_t compile_options, @@ -478,6 +479,8 @@ class cstream : public clr_object_base { cstream(const nncase_stream_mt_t *mt, void *handle) { obj_ = nncase_clr_api()->stream_create(mt, handle); } + + ~cstream() { nncase_clr_api()->handle_dispose(obj_.get()); } }; class compile_options : public clr_object_base { diff --git a/src/Nncase.Compiler/Interop/CApi.cs b/src/Nncase.Compiler/Interop/CApi.cs index 69bacc8397..2c0451d14e 100644 --- a/src/Nncase.Compiler/Interop/CApi.cs +++ b/src/Nncase.Compiler/Interop/CApi.cs @@ -41,6 +41,7 @@ public unsafe struct CApiMT public delegate* unmanaged ArrayGetItemPtr; public delegate* unmanaged ArrayGetLengthPtr; public delegate* unmanaged CalibrationDatasetProviderCreatePtr; + public delegate* unmanaged ClrHandleDisposePtr; public delegate* unmanaged ClrHandleFreePtr; public delegate* unmanaged CompileOptionsCreatePtr; public delegate* unmanaged CompileOptionsSetInputFilePtr; @@ -112,6 +113,7 @@ public static void Initialize(CApiMT* mt) mt->ArrayGetItemPtr = &ArrayGetItem; mt->ArrayGetLengthPtr = &ArrayGetLength; mt->CalibrationDatasetProviderCreatePtr = &CalibrationDatasetProviderCreate; + mt->ClrHandleDisposePtr = &ClrHandleDispose; mt->ClrHandleFreePtr = &ClrHandleFree; mt->CompileOptionsCreatePtr = &CompileOptionsCreate; mt->CompileOptionsSetInputFilePtr = &CompileOptionsSetInputFile; @@ -225,6 +227,12 @@ private static IntPtr CalibrationDatasetProviderCreate(IntPtr datasetHandle, nui return GCHandle.ToIntPtr(GCHandle.Alloc(new CCalibrationDatasetProvider(samples, (int)samplesCount))); } + [UnmanagedCallersOnly] + private static void ClrHandleDispose(IntPtr handle) + { + Get(handle).Dispose(); + } + [UnmanagedCallersOnly] private static void ClrHandleFree(IntPtr handle) {