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

Jean-Michel.Perraud at csiro.au Jean-Michel.Perraud at csiro.au
Sun Jun 16 11:21:39 UTC 2013


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.

// snip
    MonoArray* ctor_params = create_array_object(build_method_parameters(ctorParams), Rf_length(ctorParams));
    MonoObject * exception, *result;
    void** ci_p = (void**)malloc(2*sizeof(void*)); // The array of parameters for CreateInstance
    ci_p[0] = create_mono_string(ns_qualified_typename);
    ci_p[1] = ctor_params;
    result = mono_runtime_invoke (method, NULL, ci_p, &exception);
// snip

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-devel-list/attachments/20130616/d8eb0ccb/attachment-0001.html>

More information about the Mono-devel-list mailing list