How to release/free IntPtr to function pointer?

我怕爱的太早我们不能终老 提交于 2019-12-11 12:16:29

问题


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:


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)



来源:https://stackoverflow.com/questions/29077562/how-to-release-free-intptr-to-function-pointer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!