[Mono-devel-list] Re: [PATCH] Improper free of return-value string pointers from unmanagedcalls

Steven Brown swbrown at ucsd.edu
Fri Jun 11 03:51:08 EDT 2004

Chris Day wrote:
> In this case ([1]) yes it is an out parameter, however the sentiment is
> still the same.

It's not the same.  'void foo(out string)' is the conceptual equivilant 
of 'string foo(void)'.  The MSDN guy (quite correctly) is saying to 
avoid 'void foo(string)' when the string is to be treated as mutable - a 
different issue entirely.

What's at issue is if the marshaller when returning from an unmanaged 
'string foo(void)' (really, 'void foo(out string)' as well) should call 
the C library's free() on the underlying 'char *'.  Microsoft's .NET 
implementation doesn't, but Marcus has pointed to a MSDN page that says 
it does.  I'd say it's a documentation bug, as it doesn't make sense for 
the marshaller to do this.  What if the string was in .rodata, on the 
stack, a COM object, or from a different allocator?  There's a lot of 
code like that (e.g., strerror).  Freeing would only work in a few 
cases, leaving folks to use IntPtr + PtrToString.. for the rest.

More information about the Mono-devel-list mailing list