[.NET] Skip serializing delegates with a disposed target

When reloading assemblies, we serialize the managed delegates so we can recreate the callables later. If the delegate's target is a GodotObject that has already been disposed, we can't serialize the delegate.

Before this change, trying to serialize one of these delegates throws an exception and prevents releasing its strong GCHandle, so the assembly can't be unloaded.

With this change, we don't serialize the delegates and release them anyway. This means some delegates may get lost on reloading assemblies, but if their target was already freed it's probably fine.
This commit is contained in:
Raul Santos 2025-02-12 19:29:30 +01:00
parent 36d90c73a8
commit 2550cdc0c4
No known key found for this signature in database
GPG key ID: B532473AE3A803E4
2 changed files with 14 additions and 2 deletions

View file

@ -690,8 +690,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();
}
}
}