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

Kornél Pál kornelpal at hotmail.com
Tue Mar 14 08:35:10 EST 2006


> I was answering the very general question quoted at the top of my post.
> That is not about a StringBuffer but about malloc and free in general,
> so that your answer was correct in the specific case of passing a
> StringBuffer as an argument (I did read that) but not in general to the
> question of whether Mono frees unmanaged pointers:
>
> char* unmanagedfunc() {
>    return malloc(5);
> }
>
> will alloc 5 bytes,
>
> extern static string unmanagedfunc();
> string s = unmanagedfunc();
>
> will marshal the string and free the unmanaged pointer.
>
> I confirmed that Mono cannot determine whether a pointer is in use by
> unmanaged code and complemented your answer with an exception where a
> pointer is in fact freed by the runtime, which has caused some severe
> problems for me in the past; don't know whether it is a bug or a feature
> but it was observed in Mono 1.1.13 on OS X. If you don't think so then
> test it yourself.

Altough this behaviour seems to be silly at least to me you seem to be
correct and in addition this seems to be the expected behaviour:
http://msdn2.microsoft.com/en-us/library/f1cf4kkz.aspx

Note that MS.NET uses CoTaskMemFree while Mono uses g_free.

Furthermore the documentation says that if you don't want the returned
string to be freed you have to use an IntPtr. This sounds very weird to me
but this is what the runtime does.

They refer COM but COM only frees BSTR and SAFEARRAY that are quaranted to
be allocated using COM functions.

Kornél



More information about the Mono-list mailing list