From 06ea441564937d97b0931aecba771b60e077b1d5 Mon Sep 17 00:00:00 2001 From: Ignacio Etcheverry Date: Wed, 1 Nov 2017 01:48:47 +0100 Subject: [PATCH] Make sure gchandle bindings are released before cleanup --- modules/mono/csharp_script.cpp | 11 +++++++++++ modules/mono/mono_gc_handle.cpp | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/modules/mono/csharp_script.cpp b/modules/mono/csharp_script.cpp index 3d91a6de6c..edb1434ad2 100644 --- a/modules/mono/csharp_script.cpp +++ b/modules/mono/csharp_script.cpp @@ -122,6 +122,9 @@ void CSharpLanguage::init() { void CSharpLanguage::finish() { + // Release gchandle bindings before finalizing mono runtime + gchandle_bindings.clear(); + if (gdmono) { memdelete(gdmono); gdmono = NULL; @@ -794,6 +797,14 @@ void *CSharpLanguage::alloc_instance_binding_data(Object *p_object) { void CSharpLanguage::free_instance_binding_data(void *p_data) { + if (GDMono::get_singleton() == NULL) { +#ifdef DEBUG_ENABLED + CRASH_COND(!gchandle_bindings.empty()); +#endif + // Mono runtime finalized, all the gchandle bindings were already released + return; + } + #ifndef NO_THREADS script_bind_lock->lock(); #endif diff --git a/modules/mono/mono_gc_handle.cpp b/modules/mono/mono_gc_handle.cpp index d3ad968135..e10e06df0e 100644 --- a/modules/mono/mono_gc_handle.cpp +++ b/modules/mono/mono_gc_handle.cpp @@ -59,6 +59,10 @@ Ref MonoGCHandle::create_weak(MonoObject *p_object) { void MonoGCHandle::release() { +#ifdef DEBUG_ENABLED + CRASH_COND(GDMono::get_singleton() == NULL); +#endif + if (!released && GDMono::get_singleton()->is_runtime_initialized()) { mono_gchandle_free(handle); released = true;