[Mono-dev] Unwanted freeing of generic types

Aleksey Kliger alklig at microsoft.com
Thu Aug 17 14:55:02 UTC 2017

Hey Neale,

I think the code below is from mono_metadata_inflate_generic_inst.

And the question is whether it’s ok to call mono_metadata_free_type on each type_argv[i] if mono_metadata_get_generic_inst was called.  I’m going to go through how I think it’s working - let me know if I messed something up:

mono_metadata_get_generic_inst does two things:
  1. It makes a provisional MonoGenericInst allocated with g_alloca, and where the generic instantiation aliases each of the elements of type_argv from mono_metadata_inflate_generic_inst.
  2. It makes a canonical MonoGenericInst allocated in a MonoImageSet’s mempool by calling mono_metadata_get_canonical_generic_inst.
  In the case where the canonical instance already exists, we just return it.  And in this case it’s okay to free the type_argv in mono_metadata_inflate_generic_inst because nothing should need those types anymore.
  In the case where the canonical instance doesn’t exist we have to create one and we call mono_metadata_type_dup which means the canonical instance gets its own fresh MonoTypes for the arguments.  And again it’s okay to free in mono_metadata_inflate_generic_inst.

What’d I miss?


On 8/16/17, 16:57, "Mono-devel-list on behalf of Neale Ferguson" <mono-devel-list-bounces at lists.dot.net<mailto:mono-devel-list-bounces at lists.dot.net> on behalf of neale at sinenomine.net<mailto:neale at sinenomine.net>> wrote:

If there is no error in the first loop do we still want to free each of the type_argv elements in cleanup? If they are these types are overwritten. Freeing type_argv is valid as the mono_class_inflate_generic_type_checked() makes a copy of the array.

        for (i = 0; i < ginst->type_argc; i++) {

                type_argv [i] = mono_class_inflate_generic_type_checked (ginst->type_argv [i], context, error);

                if (!mono_error_ok (error))

                        goto cleanup;



        nginst = mono_metadata_get_generic_inst (ginst->type_argc, type_argv);


        for (i = 0; i < count; i++)

                mono_metadata_free_type (type_argv [i]);

        g_free (type_argv);

        return nginst;

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.dot.net/pipermail/mono-devel-list/attachments/20170817/996bbf1c/attachment.html>

More information about the Mono-devel-list mailing list