Monday, 15 August 2011

c# - How to release/free IntPtr to function pointer? -


I've got two original DLL (without sources with external methods): Init and < code.> 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 exile IntPtr InitNative (); [DllImport (@ "myDLL.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl, charset = CharSet.Ansi, EntryPoint = "EntryPoint # 2", ExactSpelling = true)] DoSomeWorkN (byte [] ArrayOut Ushort private static exile , [IN] intact inithendal); Private Readonly IntPtr _initHandle; Public MyClass () {_initHandle = InitNative (); } Secure Override Byte [] DoSomeWork () {... DoSomeWorkN (buffOut, _initHandle); ...} Remove public override zero () {// ??? }  

I have tried:

  Marshall.FreeHejiloble (_initHandle); // throws exception: invalid access to storage location. (HRESULT: exception 0x800703E6) "} Marshall. Frikotskm (_initHandle); // access violation exception Marshall Frebrad (the _initHandle); // exception does not throw, but does not work (after calling the method IntPtr isn not IntPtr.Zero)  

so, how to apply the correct disposal of _initHandle?

Your library should / will give you a third method:

  Zero intro (intptr handle);  

You do not know If memory was allocated, then the memory was allocated through malloc C . You can not even freely release it. Even worse, if it is a C ++ object Only C ++ can correctly revert it (called the perfect Destroyer). This library should be done to give you a method to free your memory.

(Technically you Can not even know what is IntPtr :-) Perhaps it is Is not an indicator Stw. This can be a number and you have nothing blank ... or it can be handle back by win32 CreateFile )

< / Html>

No comments:

Post a Comment