I am using some unmanaged code that is returning pointers (IntPtr) to large image objects. I use the references but after I am finished with the images, I need to free that memory referenced by the pointers. Currently, the only thing that frees the memory is to shutdown my entire app. I need to be able to free that memory from inside my application.
Here is the call to that allocates the memory. hbitmap
is the pointer that is returned and needs to be deallocated.
[DllImport("twain_32.dll", EntryPoint = "#1")]
public static extern TwainResult DsImageTransfer(
[In, Out] Identity origin, [In] Identity dest, DataGroup dg,
DataArgumentType dat, Message msg, ref IntPtr hbitmap);
You need to use the specific memory allocator mechanism that was used to allocate the memory in the first place.
So, if you were using COM and the IMalloc
interface to allocate the memory, then you have to pass the IntPtr
back to the Free
method on that implementation in order to free the memory allocated.
If you are indeed using the COM allocator that is returned by a call to CoGetMalloc
, then you can call the static FreeCoTaskMem
method on the Marshal
class.
The Marshal
class also has a method for freeing memory that is allocated through a call to LocalAlloc
called FreeHGlobal
.
However, and this is a common case, if the memory was allocated by the new
operator in C++, or a call to malloc
in C, then you have to expose a function in unmanaged code through interop which will free the memory appropriately.
In the case of C++, you would expose a function that takes a pointer and simply calls delete
on that pointer. In the case of malloc
, you would create a function that takes a pointer, and calls free
on that pointer.
In specific regards to your question, it would seem that DsImageTransfer
is a vendor-specific API (one that doesn't have much discoverability on the web either, I'm afraid), so more information is needed about that specific API function and how it allocates memory. Just knowing the handle type (an HBITMAP
in this case) doesn't give any indication as to how it's allocated. It can be allocated with all the mechanisms mentioned above.
Assuming it is creating the HBITMAP
using GDI Object api functions (specifically, the CreateBitmap
function), then you could use the DeleteObject
function to release the handle (as per the documentation page for the GDI Object API functions).
That would depend on how that memory was allocated. The Marshal class has methods for deallocating memory allocated through the common interop allocation patterns, like FreeCoTaskMem. If the unmanaged code uses a non Interop compatible way of allocating, then you cannot interop with it.
Updated
If I would venture a guess, the function #1 you invoke in twain_32.dll is the DS_ENTRY function in a TWAIN provider. The Twain specifications call out the memory resource management protocol:
Memory Management in TWAIN 2.0 and Higher
TWAIN requires Applications and Sources to manage each other’s memory. The chief problem is guaranteeing agreement on the API’s to use. TWAIN 2.0 introduces four new functions that are obtained from the Source Manager through DAT_ENTRYPOINT.
TW_HANDLE PASCAL DSM_MemAllocate (TW_UINT32)
PASCAL DSM_MemFree (TW_HANDLE)
TW_MEMREF PASCAL DSM_MemLock(TW_HANDLE)
void PASCAL DSM_MemUnlock(TW_HANDLE)
These functions correspond to the WIN32 Global Memory functions mentioned in previous versions of the TWAIN Specification:
GlobalAlloc
,GlobalFree
,GlobalLock
,GlobalUnlock
On MacOS/X these functions callNewPtrClear
andDisposePtr
. The lock and unlock functions are no-ops, but they still must be called. TWAIN 2.0 compliant Applications and Sources must use these calls on all platforms (Windows, MacOS/X and Linux). The Source Manager takes the responsibility to make sure that all components are using the same memory management API’s.
So to free resources you're supposed to call DSM_MemFree, which supposedly on Win32 platforms would be implemented through GlobalFree, or Marshal.FreeHGlobal.
Since this is mostly speculation on my part, you better validate with the specs of the specific TWAIN implementation you use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With