Repository: godot
Issue: Opening and closing project with mono build cause memory leak
Labels: bug,topic:dotnet

### Godot version

3.5.beta.mono.custom_build. 2f942ab84

### System information

Ubuntu 21.04, Intel HD 3000

### Issue description

Opening and closing editor even with empty project cause this memory leak
```
Direct leak of 256 byte(s) in 1 object(s) allocated from:
    #0 0x7fc0bda9e808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144
    #1 0x12b13c83 in Memory::alloc_static(unsigned long, bool) core/os/memory.cpp:75
    #2 0x12b13b94 in operator new(unsigned long, char const*) core/os/memory.cpp:40
    #3 0x241f59c in GDMonoClass::get_all_delegates() modules/mono/mono_gd/gd_mono_class.cpp:455
    #4 0x3b66170 in CSharpScript::load_script_signals(GDMonoClass*, GDMonoClass*) modules/mono/csharp_script.cpp:2457
    #5 0x3b74e1f in CSharpScript::initialize_for_managed_type(Ref<CSharpScript>, GDMonoClass*, GDMonoClass*) modules/mono/csharp_script.cpp:2860
    #6 0x3b72263 in CSharpScript::create_for_managed_type(GDMonoClass*, GDMonoClass*) modules/mono/csharp_script.cpp:2797
    #7 0x3d80849 in GDMonoInternals::tie_managed_to_unmanaged(_MonoObject*, Object*) modules/mono/mono_gd/gd_mono_internals.cpp:106
    #8 0x3d17e45 in godot_icall_VBoxContainer_Ctor(_MonoObject*) modules/mono/glue/mono_glue.gen.cpp:10716
    #9 0x47255307  (<unknown module>)
    #10 0x7fc0bd321011  (/lib/libmonosgen-2.0.so.1+0x41011)
    #11 0x7fc0bd52957d  (/lib/libmonosgen-2.0.so.1+0x24957d)

Indirect leak of 288 byte(s) in 6 object(s) allocated from:
    #0 0x7fc0bda9ea06 in __interceptor_calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:153
    #1 0x7fc0bd61176e in monoeg_g_calloc (/lib/libmonosgen-2.0.so.1+0x33176e)

Indirect leak of 192 byte(s) in 1 object(s) allocated from:
    #0 0x7fc0bda9e808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144
    #1 0x12b13c83 in Memory::alloc_static(unsigned long, bool) core/os/memory.cpp:75
    #2 0x12b13b94 in operator new(unsigned long, char const*) core/os/memory.cpp:40
    #3 0x241c8f9 in GDMonoClass::get_method(_MonoMethod*, StringName const&, unsigned short) modules/mono/mono_gd/gd_mono_class.cpp:329
    #4 0x241c565 in GDMonoClass::get_method(_MonoMethod*) modules/mono/mono_gd/gd_mono_class.cpp:310
    #5 0x3b66b16 in CSharpScript::_get_signal(GDMonoClass*, GDMonoClass*, Vector<CSharpScript::Argument>&) modules/mono/csharp_script.cpp:2482
    #6 0x3b663c7 in CSharpScript::load_script_signals(GDMonoClass*, GDMonoClass*) modules/mono/csharp_script.cpp:2463
    #7 0x3b74e1f in CSharpScript::initialize_for_managed_type(Ref<CSharpScript>, GDMonoClass*, GDMonoClass*) modules/mono/csharp_script.cpp:2860
    #8 0x3b72263 in CSharpScript::create_for_managed_type(GDMonoClass*, GDMonoClass*) modules/mono/csharp_script.cpp:2797
    #9 0x3d80849 in GDMonoInternals::tie_managed_to_unmanaged(_MonoObject*, Object*) modules/mono/mono_gd/gd_mono_internals.cpp:106
    #10 0x3d17e45 in godot_icall_VBoxContainer_Ctor(_MonoObject*) modules/mono/glue/mono_glue.gen.cpp:10716
    #11 0x47255307  (<unknown module>)
    #12 0x7fc0bd321011  (/lib/libmonosgen-2.0.so.1+0x41011)
    #13 0x7fc0bd52957d  (/lib/libmonosgen-2.0.so.1+0x24957d)

Indirect leak of 128 byte(s) in 1 object(s) allocated from:
    #0 0x7fc0bda9ec3e in __interceptor_realloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:163
    #1 0x7fc0bd611682 in monoeg_realloc (/lib/libmonosgen-2.0.so.1+0x331682)

Indirect leak of 80 byte(s) in 1 object(s) allocated from:
    #0 0x7fc0bda9e808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144
    #1 0x12b13c83 in Memory::alloc_static(unsigned long, bool) core/os/memory.cpp:75
    #2 0x243ac18 in HashMap<GDMonoClass::MethodKey, GDMonoMethod*, GDMonoClass::MethodKey::Hasher, HashMapComparatorDefault<GDMonoClass::MethodKey>, (unsigned char)3, (unsigned char)8>::Element** memnew_arr_template<HashMap<GDMonoClass::MethodKey, GDMonoMethod*, GDMonoClass::MethodKey::Hasher, HashMapComparatorDefault<GDMonoClass::MethodKey>, (unsigned char)3, (unsigned char)8>::Element*>(unsigned long, char const*) (/home/rafal/Pobrane/linux3x-editor-mono-sanitizers/godot.x11.tools.64s.mono+0x243ac18)
    #3 0x243039e in HashMap<GDMonoClass::MethodKey, GDMonoMethod*, GDMonoClass::MethodKey::Hasher, HashMapComparatorDefault<GDMonoClass::MethodKey>, (unsigned char)3, (unsigned char)8>::make_hash_table() (/home/rafal/Pobrane/linux3x-editor-mono-sanitizers/godot.x11.tools.64s.mono+0x243039e)
    #4 0x24289b3 in HashMap<GDMonoClass::MethodKey, GDMonoMethod*, GDMonoClass::MethodKey::Hasher, HashMapComparatorDefault<GDMonoClass::MethodKey>, (unsigned char)3, (unsigned char)8>::set(HashMap<GDMonoClass::MethodKey, GDMonoMethod*, GDMonoClass::MethodKey::Hasher, HashMapComparatorDefault<GDMonoClass::MethodKey>, (unsigned char)3, (unsigned char)8>::Pair const&) (/home/rafal/Pobrane/linux3x-editor-mono-sanitizers/godot.x11.tools.64s.mono+0x24289b3)
    #5 0x24237e6 in HashMap<GDMonoClass::MethodKey, GDMonoMethod*, GDMonoClass::MethodKey::Hasher, HashMapComparatorDefault<GDMonoClass::MethodKey>, (unsigned char)3, (unsigned char)8>::set(GDMonoClass::MethodKey const&, GDMonoMethod* const&) (/home/rafal/Pobrane/linux3x-editor-mono-sanitizers/godot.x11.tools.64s.mono+0x24237e6)
    #6 0x241ca24 in GDMonoClass::get_method(_MonoMethod*, StringName const&, unsigned short) modules/mono/mono_gd/gd_mono_class.cpp:330
    #7 0x241c565 in GDMonoClass::get_method(_MonoMethod*) modules/mono/mono_gd/gd_mono_class.cpp:310
    #8 0x3b66b16 in CSharpScript::_get_signal(GDMonoClass*, GDMonoClass*, Vector<CSharpScript::Argument>&) modules/mono/csharp_script.cpp:2482
    #9 0x3b663c7 in CSharpScript::load_script_signals(GDMonoClass*, GDMonoClass*) modules/mono/csharp_script.cpp:2463
    #10 0x3b74e1f in CSharpScript::initialize_for_managed_type(Ref<CSharpScript>, GDMonoClass*, GDMonoClass*) modules/mono/csharp_script.cpp:2860
    #11 0x3b72263 in CSharpScript::create_for_managed_type(GDMonoClass*, GDMonoClass*) modules/mono/csharp_script.cpp:2797
    #12 0x3d80849 in GDMonoInternals::tie_managed_to_unmanaged(_MonoObject*, Object*) modules/mono/mono_gd/gd_mono_internals.cpp:106
    #13 0x3d17e45 in godot_icall_VBoxContainer_Ctor(_MonoObject*) modules/mono/glue/mono_glue.gen.cpp:10716
    #14 0x47255307  (<unknown module>)
    #15 0x7fc0bd321011  (/lib/libmonosgen-2.0.so.1+0x41011)
    #16 0x7fc0bd52957d  (/lib/libmonosgen-2.0.so.1+0x24957d)

Indirect leak of 56 byte(s) in 1 object(s) allocated from:
    #0 0x7fc0bda9e808 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:144
    #1 0x12b13c83 in Memory::alloc_static(unsigned long, bool) core/os/memory.cpp:75
    #2 0x12b13b94 in operator new(unsigned long, char const*) core/os/memory.cpp:40
    #3 0x243082d in HashMap<GDMonoClass::MethodKey, GDMonoMethod*, GDMonoClass::MethodKey::Hasher, HashMapComparatorDefault<GDMonoClass::MethodKey>, (unsigned char)3, (unsigned char)8>::create_element(GDMonoClass::MethodKey const&) (/home/rafal/Pobrane/linux3x-editor-mono-sanitizers/godot.x11.tools.64s.mono+0x243082d)
    #4 0x2428b17 in HashMap<GDMonoClass::MethodKey, GDMonoMethod*, GDMonoClass::MethodKey::Hasher, HashMapComparatorDefault<GDMonoClass::MethodKey>, (unsigned char)3, (unsigned char)8>::set(HashMap<GDMonoClass::MethodKey, GDMonoMethod*, GDMonoClass::MethodKey::Hasher, HashMapComparatorDefault<GDMonoClass::MethodKey>, (unsigned char)3, (unsigned char)8>::Pair const&) (/home/rafal/Pobrane/linux3x-editor-mono-sanitizers/godot.x11.tools.64s.mono+0x2428b17)
    #5 0x24237e6 in HashMap<GDMonoClass::MethodKey, GDMonoMethod*, GDMonoClass::MethodKey::Hasher, HashMapComparatorDefault<GDMonoClass::MethodKey>, (unsigned char)3, (unsigned char)8>::set(GDMonoClass::MethodKey const&, GDMonoMethod* const&) (/home/rafal/Pobrane/linux3x-editor-mono-sanitizers/godot.x11.tools.64s.mono+0x24237e6)
    #6 0x241ca24 in GDMonoClass::get_method(_MonoMethod*, StringName const&, unsigned short) modules/mono/mono_gd/gd_mono_class.cpp:330
    #7 0x241c565 in GDMonoClass::get_method(_MonoMethod*) modules/mono/mono_gd/gd_mono_class.cpp:310
    #8 0x3b66b16 in CSharpScript::_get_signal(GDMonoClass*, GDMonoClass*, Vector<CSharpScript::Argument>&) modules/mono/csharp_script.cpp:2482
    #9 0x3b663c7 in CSharpScript::load_script_signals(GDMonoClass*, GDMonoClass*) modules/mono/csharp_script.cpp:2463
    #10 0x3b74e1f in CSharpScript::initialize_for_managed_type(Ref<CSharpScript>, GDMonoClass*, GDMonoClass*) modules/mono/csharp_script.cpp:2860
    #11 0x3b72263 in CSharpScript::create_for_managed_type(GDMonoClass*, GDMonoClass*) modules/mono/csharp_script.cpp:2797
    #12 0x3d80849 in GDMonoInternals::tie_managed_to_unmanaged(_MonoObject*, Object*) modules/mono/mono_gd/gd_mono_internals.cpp:106
    #13 0x3d17e45 in godot_icall_VBoxContainer_Ctor(_MonoObject*) modules/mono/glue/mono_glue.gen.cpp:10716
    #14 0x47255307  (<unknown module>)
    #15 0x7fc0bd321011  (/lib/libmonosgen-2.0.so.1+0x41011)
    #16 0x7fc0bd52957d  (/lib/libmonosgen-2.0.so.1+0x24957d)

SUMMARY: AddressSanitizer: 6332 byte(s) leaked in 229 allocation(s).
```

### Steps to reproduce

Just open any project

Prebuilt binaries with leak sanitizer on Linux are available here - https://github.com/qarmin/GodotBuilds/actions

### Minimal reproduction project

_No response_