From 844fad2cedb7a9bebd906bbab9822f56a5f6bd09 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sat, 8 Jan 2022 18:29:13 +0100 Subject: [PATCH] Fix crash when animation has anim events and scripting is disposed before content --- Source/Engine/Content/Assets/Animation.cpp | 18 ++++++++++++++++++ Source/Engine/Content/Assets/Animation.h | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/Source/Engine/Content/Assets/Animation.cpp b/Source/Engine/Content/Assets/Animation.cpp index 789153c21..8788f1466 100644 --- a/Source/Engine/Content/Assets/Animation.cpp +++ b/Source/Engine/Content/Assets/Animation.cpp @@ -502,6 +502,24 @@ void Animation::OnSkinnedModelUnloaded(Asset* obj) MappingCache.Remove(i); } +void Animation::OnScriptingDispose() +{ + // Dispose any events to prevent crashes (scripting is released before content) + for (auto& e : Events) + { + for (auto& k : e.Second.GetKeyframes()) + { + if (k.Value.Instance) + { + Delete(k.Value.Instance); + k.Value.Instance = nullptr; + } + } + } + + BinaryAsset::OnScriptingDispose(); +} + Asset::LoadResult Animation::load() { // Get stream with animations data diff --git a/Source/Engine/Content/Assets/Animation.h b/Source/Engine/Content/Assets/Animation.h index 8c9b9c095..80085e392 100644 --- a/Source/Engine/Content/Assets/Animation.h +++ b/Source/Engine/Content/Assets/Animation.h @@ -163,6 +163,11 @@ private: void OnSkinnedModelUnloaded(Asset* obj); +public: + + // [BinaryAsset] + void OnScriptingDispose() override; + protected: // [BinaryAsset]