[Mono-dev] Embedding Mono + SWIG string return values leads to bad free

Jonathan Chambers joncham at gmail.com
Thu May 15 09:52:13 EDT 2008


Looking at the marshal.c code, this may be a problem in the runtime. We
allocate return strings using mono_marshal_alloc (evaluates to
CoTaskMemAlloc on Windows). However, we always free returned strings using
g_free on all platforms. I suppose we should use mono_marshal_free instead?
Please review attached patch. Not sure if this will fix your issue, but it
may.

Code is contributed under MIT/X11 license.

Thanks,
Jonathan

On Fri, Apr 25, 2008 at 7:16 PM, Miguel de Icaza <miguel at ximian.com> wrote:

> [resending, email server not working]
> On Thu, 2008-04-24 at 18:33 -0400, Miguel de Icaza wrote:
> > Hello,
> >
> > > The returned string is passed back to the SWIG C# class and used
> > > (successfully) and then later the GC tries to collect it and fails.
> > > The C# delegate is just something like
> >
> > The GC does not collect strings returned from P/Invoke, but it will free
> > return values.
> >
> > If you are returning strings that are now malloc()ed (and ownership is
> > transferred to the caller), you may want to change the P/Invoke
> > signature to return an IntPtr, and then use Marshal.PtrToStringAuto
> >
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20080515/8ca8a67e/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: stringfree.diff
Type: text/x-diff
Size: 973 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20080515/8ca8a67e/attachment.bin 


More information about the Mono-devel-list mailing list