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

Chris Day chrisd at monkey.biz
Fri Jun 11 02:51:55 EDT 2004


In this case ([1]) yes it is an out parameter, however the sentiment is
still the same.  If the function requires a string to get to managed
land it should use an IntPtr then Marshal.PtrToString* to movie it
across, and then if required deallocate the string by p/invoking a
free() function of some sort and passing in the IntPtr.  If you are
p/invoking a function it is up to you as a developer to know whether a
string (the IntPtr) must be freed, not the framework.

If however the function expects a buffer, to write a string into, MS
recommends that a StringBuilder is used.

Chris

> -----Original Message-----
> From: Steven Brown [mailto:swbrown at ucsd.edu] 
> Sent: Friday, 11 June 2004 4:36 PM
> To: Chris Day
> Cc: mono-devel-list at lists.ximian.com
> Subject: Re: [PATCH] Improper free of return-value string 
> pointers from unmanagedcalls
> 
> Chris Day wrote:
> 
> > Just as an FYI, Microsoft recommends [1] that any return or out 
> > parameters for strings (char*, wchar_t*, etc) use an IntPtr then use
> > Marshal.PtrToString* [2] to copy it into managed land.  If the 
> > function allocates the string it is then up to you to 
> import a free() 
> > function which you can call with the IntPtr to deallocate 
> the string.
> > 
> > Chris
> > 
> > [1]
> > 
> http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=ON3LnWFHBHA.18
> > 68
> > %40tkmsftngp03
> > [2]
> > 
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref
> > /h 
> > 
> tml/frlrfsystemruntimeinteropservicesmarshalclassptrtostringautotopic1
> > .a
> > sp
> 
> In [1], he's talking about out parameters, not return 
> parameters (although he calls them return parameters).  It's 
> a different beast, as the guy's code is written to expect an 
> immutable string to be modified.
> 
> 
> 



More information about the Mono-devel-list mailing list