[Mono-list] Mono.Unix.Native.Syscall.readlink memory corruption

Andreas Färber andreas.faerber at web.de
Tue Mar 14 07:08:53 EST 2006


>> On a related note:  if a C function mallocs some memory and returns a
>> pointer
>> to that memory, does p/invoke free the memory once marshalling is
>> complete?
>
> No. There is simply no way to automatically determine whether it
> should be
> freed and there is even less chance to determine how (using what
> function)
> it should be freed.
>
> If you want to do so you have to use an IntPtr that you can free
> manually.
> Note that you can alternatively use a custom marshaler but you have to do
> the same with a more complex and probably more proper design.
This is not entirely true. A returned string is actually automatically
freed when marshalled as string (as opposed to IntPtr). In that case
Mono does not determine whether it should be freed, it simply frees it.

Andreas


More information about the Mono-list mailing list