[Mono-dev] What is the best practice for disposing of transient MonoObject in an embedding scenario

Robert Jordan robertj at gmx.net
Mon Jun 17 11:38:23 UTC 2013

On 16.06.2013 13:21, Jean-Michel.Perraud at csiro.au wrote:
> Hi,
> I'd like some guidance on how to free memory in an embedded Mono
> scenario.
> I 'monkeyed' a few things started from the embedded samples in the
> mono codebase, but they are limited to g_free calls on UTF8 strings.
> I think I figured out I needed to use mono_gchandle_new and
> mono_gchandle_free to 'pin' (resp. 'free') MonoObject from the
> embedding application when they re not in use anymore outside of the
> CLR. Nevertheless the fate of transient objects is a bit of a
> mystery.
> Say I have a C# function: public static object CreateInstance(string
> typename, object[] ctorArgs)
> Which I am calling from C with the code below. What should I do with
> the transient MonoArray 'ctor_params' before leaving the function? is
> mono_free to call in this case, or would the garbage collector do its
> job just fine as it is.

As long as you store transient MonoObjects* in local variables on the
stack, you don't need to care about their lifetime.

MonoObjects* stored on the C/C++ heap or static segments must be
kept alive with a gchandle.


More information about the Mono-devel-list mailing list