Fix crash after scripting hot-reload in editor due to cached class attribute objects

#1108
This commit is contained in:
Wojtek Figat
2023-05-19 13:53:49 +02:00
parent 7b8c013918
commit 144b72109b
5 changed files with 33 additions and 0 deletions

View File

@@ -45,6 +45,11 @@ public:
/// </summary>
static void UnloadEngine();
#if USE_EDITOR
// Called by Scripting in a middle of hot-reload (after unloading modules but before loading them again).
static void OnMidHotReload();
#endif
public:
/// <summary>
/// Utilities for C# object management.

View File

@@ -299,6 +299,17 @@ void MCore::UnloadEngine()
ShutdownHostfxr();
}
#if USE_EDITOR
void MCore::OnMidHotReload()
{
// Clear any cached class attributes (see https://github.com/FlaxEngine/FlaxEngine/issues/1108)
for (auto e : CachedClassHandles)
e.Value->_attributes.Clear();
}
#endif
MObject* MCore::Object::Box(void* value, const MClass* klass)
{
static void* BoxValuePtr = GetStaticMethodPointer(TEXT("BoxValue"));

View File

@@ -714,6 +714,14 @@ void MCore::UnloadEngine()
#endif
}
#if USE_EDITOR
void MCore::OnMidHotReload()
{
}
#endif
MObject* MCore::Object::Box(void* value, const MClass* klass)
{
return mono_value_box(mono_domain_get(), klass->GetNative(), value);

View File

@@ -59,6 +59,14 @@ void MCore::UnloadEngine()
MRootDomain = nullptr;
}
#if USE_EDITOR
void MCore::OnMidHotReload()
{
}
#endif
MObject* MCore::Object::Box(void* value, const MClass* klass)
{
return nullptr;

View File

@@ -665,6 +665,7 @@ void Scripting::Reload(bool canTriggerSceneReload)
modules.Clear();
_nonNativeModules.ClearDelete();
_hasGameModulesLoaded = false;
MCore::OnMidHotReload();
// Give GC a try to cleanup old user objects and the other mess
MCore::GC::Collect();