diff --git a/modules/mono/csharp_script.cpp b/modules/mono/csharp_script.cpp index da037005c0..991379b5f3 100644 --- a/modules/mono/csharp_script.cpp +++ b/modules/mono/csharp_script.cpp @@ -685,8 +685,14 @@ void CSharpLanguage::reload_assemblies(bool p_soft_reload) { if (success) { ManagedCallable::instances_pending_reload.insert(managed_callable, serialized_data); - } else if (OS::get_singleton()->is_stdout_verbose()) { - OS::get_singleton()->print("Failed to serialize delegate\n"); + } else { + if (OS::get_singleton()->is_stdout_verbose()) { + OS::get_singleton()->print("Failed to serialize delegate.\n"); + } + + // We failed to serialize the delegate but we still have to release it; + // otherwise, we won't be able to unload the assembly. + managed_callable->release_delegate_handle(); } } } diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/DelegateUtils.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/DelegateUtils.cs index 7a88fea5f1..227bceacd7 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/DelegateUtils.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/DelegateUtils.cs @@ -166,6 +166,12 @@ namespace Godot } case GodotObject godotObject: { + if (!GodotObject.IsInstanceValid(godotObject)) + { + // If the delegate's target has been freed we can't serialize it. + return false; + } + using (var stream = new MemoryStream()) using (var writer = new BinaryWriter(stream)) {