How to release/free IntPtr to function pointer?

2019-08-06 11:16发布

I've got native DLL (without sources) with two extern methods: Init and DoSomeWork.

Here is my class-wrapper:

public class MyClass : IDisposable
{
[DllImport(@"myDLL.dll",
SetLastError = true,
CallingConvention = CallingConvention.Cdecl,
CharSet = CharSet.Ansi,
EntryPoint = "EntryPoint#1",
ExactSpelling = true)]
private static extern IntPtr InitNative();

[DllImport(@"myDLL.dll",
SetLastError = true,
CallingConvention = CallingConvention.Cdecl,
CharSet = CharSet.Ansi,
EntryPoint = "EntryPoint#2",
ExactSpelling = true)]
private static extern ushort DoSomeWorkN(byte[] arrayOut, [In] IntPtr initHandle);

private readonly IntPtr _initHandle;

public MyClass()
{
    _initHandle = InitNative();
}

protected override byte[] DoSomeWork()
{ 
  ...
    DoSomeWorkN(buffOut, _initHandle);
  ...
}

public override void Dispose()
{
    //???
}

I've tried:

Marshal.FreeHGlobal(_initHandle); //throws exception: Invalid access to memory location. (Exception from HRESULT: 0x800703E6)"}
Marshal.FreeCoTaskMem(_initHandle); //throws Access violation exception
Marshal.FreeBSTR(_initHandle); //doesn't throw exception, but doesn't work (after calling that method IntPtr isn't IntPtr.Zero)

So, how to implement correct disposing of _initHandle?

1条回答
做个烂人
2楼-- · 2019-08-06 11:36

Your library should/must give you a third method:

void Free(IntPtr handle);

You can't know how the memory was allocated. If the memory was allocated through C malloc you can't even easily free it. Even worse, if it is a C++ object, only C++ can correctly deallocate it (calling the correct destructors). It must be the library to give you a method to free its memory.

(technically you can't even know what the IntPtr is :-) Perhaps it isn't really a pointer. It could be a number and you don't have to free anything... or it could be an HANDLE returned by Win32 CreateFile)

查看更多
登录 后发表回答