How to free heap allocated my malloc?

G

Guest

Is it there a way in C# to free heap memory that has been allocated by an
unmanaged component with malloc()?

I get an exception when trying to use Marshal.FreeCoTaskMem() on the managed
pointer (of type IntPtr) returned from un-managed code as an "out" parameter.
In the C-dll this parameter is a double-pointer of my custom structure.

I think the reason is that there is no CoTaskMemAlloc() in the ANSI-C-style
DLL for allocating this pointer, just malloc().

Any suggestions for a good book or another source on PInvoke and Marshalling
different data types (including pointers) back and forth between managed and
unmanaged code (like C-style APIs)?

Any quide-lines for C# interoperability with ANSI-C code for multi-platforms
would be appreciated.


Valentina
 
M

Michael Nedzelsky

It seems you must implement a special function my_free in your own unmanaged
code and call it from managed code.
Michael
 
G

Guest

THANKS MICHAEL.
It works very well and I can use unmanaged pointers only!

Valentina
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top