How to free IntPtr in C #?

How to free ptrSentFromPinvokedDLL?

IntPtr ptrSentFromPinvokedDLL= IntPtr.Zero; int resultFromVendor = CallVendorDll(ref ptrSentFromPinvokedDLL); resultFromVendor = DoMoreWorkFromVendorDLL( ptrSentFromPinvokedDLL, "workonthis"); // Free ptrSentFromPinvokedDLLhere 
+7
c # interop pinvoke
Jan 29 '10 at 0:40
source share
2 answers

Ideally, either the provider is worried about this, or there is a provider function to free up memory. If not, you need to know how the provider allocated the memory. For example, if a provider allocated memory using LocalAlloc in kernel32.dll , you can free memory using Marshal.FreeHGlobal(IntPtr) . Similarly, if the CoTaskMemAlloc memory CoTaskMemAlloc , then Marshal.FreeCoTaskMem(IntPtr) will be used to free memory. Therefore, check the documentation and act accordingly.

For reference, here

+9
Jan 29 '10 at 1:01
source share

There is no way to find out how to free it, or even need to free it without seeing the code inside CallVendorDll . (Or some documentation)

+1
Jan 29 '10 at 1:02
source share



All Articles