From ea737db43cba15e5d57442a647e9abcb91ed4ea7 Mon Sep 17 00:00:00 2001 From: Ignacio Etcheverry Date: Mon, 13 Jan 2020 20:58:46 +0100 Subject: Mono/C#: Fix _update_exports() leaking temporary Object/Node instances --- modules/mono/csharp_script.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/modules/mono/csharp_script.cpp b/modules/mono/csharp_script.cpp index d82e78d080..c708fd412d 100644 --- a/modules/mono/csharp_script.cpp +++ b/modules/mono/csharp_script.cpp @@ -2243,7 +2243,11 @@ bool CSharpScript::_update_exports() { MonoException *ctor_exc = NULL; ctor->invoke(tmp_object, NULL, &ctor_exc); + Object *tmp_native = GDMonoMarshal::unbox(CACHED_FIELD(GodotObject, ptr)->get_value(tmp_object)); + if (ctor_exc) { + // TODO: Should we free 'tmp_native' if the exception was thrown after its creation? + MonoGCHandle::free_handle(tmp_pinned_gchandle); tmp_object = NULL; @@ -2322,6 +2326,15 @@ bool CSharpScript::_update_exports() { MonoGCHandle::free_handle(tmp_pinned_gchandle); tmp_object = NULL; + + if (tmp_native && !Object::cast_to(tmp_native)) { + Node *node = Object::cast_to(tmp_native); + if (node && node->is_inside_tree()) { + ERR_PRINTS("Temporary instance was added to the scene tree."); + } else { + memdelete(tmp_native); + } + } } placeholder_fallback_enabled = false; -- cgit v1.2.3