diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2023-11-24 00:00:23 +0100 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2023-11-24 00:00:23 +0100 |
commit | e6c8d40d106214c1ab4df23e5315b9f1f659ce70 (patch) | |
tree | 3c6c7a651a72527a1ab7fc827c104a49363b5422 | |
parent | ce3bac5f55507a81fb76eb0fc0b37693f663dad0 (diff) | |
parent | 1c0a0f5fddc523cb176742fb0110fcc23d5f316a (diff) | |
download | redot-engine-e6c8d40d106214c1ab4df23e5315b9f1f659ce70.tar.gz |
Merge pull request #85266 from KoBeWi/copy_more_RAM
Fix TrackCache memory crash
-rw-r--r-- | scene/animation/animation_mixer.cpp | 70 | ||||
-rw-r--r-- | scene/animation/animation_mixer.h | 13 |
2 files changed, 76 insertions, 7 deletions
diff --git a/scene/animation/animation_mixer.cpp b/scene/animation/animation_mixer.cpp index e23fc56a23..522fc2db32 100644 --- a/scene/animation/animation_mixer.cpp +++ b/scene/animation/animation_mixer.cpp @@ -2138,3 +2138,73 @@ AnimationMixer::AnimationMixer() { AnimationMixer::~AnimationMixer() { } + +void AnimatedValuesBackup::set_data(const HashMap<NodePath, AnimationMixer::TrackCache *> p_data) { + clear_data(); + + for (const KeyValue<NodePath, AnimationMixer::TrackCache *> &E : p_data) { + data.insert(E.key, get_cache_copy(E.value)); + } +} + +HashMap<NodePath, AnimationMixer::TrackCache *> AnimatedValuesBackup::get_data() const { + HashMap<NodePath, AnimationMixer::TrackCache *> ret; + for (const KeyValue<NodePath, AnimationMixer::TrackCache *> &E : data) { + ret.insert(E.key, get_cache_copy(E.value)); + } + return ret; +} + +void AnimatedValuesBackup::clear_data() { + for (KeyValue<NodePath, AnimationMixer::TrackCache *> &K : data) { + memdelete(K.value); + } + data.clear(); +} + +AnimationMixer::TrackCache *AnimatedValuesBackup::get_cache_copy(AnimationMixer::TrackCache *p_cache) const { + switch (p_cache->type) { + case Animation::TYPE_VALUE: { + AnimationMixer::TrackCacheValue *src = static_cast<AnimationMixer::TrackCacheValue *>(p_cache); + AnimationMixer::TrackCacheValue *tc = memnew(AnimationMixer::TrackCacheValue); + memcpy((void *)tc, (void *)src, sizeof(AnimationMixer::TrackCacheValue)); + return tc; + } + + case Animation::TYPE_POSITION_3D: + case Animation::TYPE_ROTATION_3D: + case Animation::TYPE_SCALE_3D: { + AnimationMixer::TrackCacheTransform *src = static_cast<AnimationMixer::TrackCacheTransform *>(p_cache); + AnimationMixer::TrackCacheTransform *tc = memnew(AnimationMixer::TrackCacheTransform); + memcpy((void *)tc, (void *)src, sizeof(AnimationMixer::TrackCacheTransform)); + return tc; + } + + case Animation::TYPE_BLEND_SHAPE: { + AnimationMixer::TrackCacheBlendShape *src = static_cast<AnimationMixer::TrackCacheBlendShape *>(p_cache); + AnimationMixer::TrackCacheBlendShape *tc = memnew(AnimationMixer::TrackCacheBlendShape); + memcpy((void *)tc, (void *)src, sizeof(AnimationMixer::TrackCacheBlendShape)); + return tc; + } + + case Animation::TYPE_BEZIER: { + AnimationMixer::TrackCacheBezier *src = static_cast<AnimationMixer::TrackCacheBezier *>(p_cache); + AnimationMixer::TrackCacheBezier *tc = memnew(AnimationMixer::TrackCacheBezier); + memcpy((void *)tc, (void *)src, sizeof(AnimationMixer::TrackCacheBezier)); + return tc; + } + + case Animation::TYPE_AUDIO: { + AnimationMixer::TrackCacheAudio *src = static_cast<AnimationMixer::TrackCacheAudio *>(p_cache); + AnimationMixer::TrackCacheAudio *tc = memnew(AnimationMixer::TrackCacheAudio); + memcpy((void *)tc, (void *)src, sizeof(AnimationMixer::TrackCacheAudio)); + return tc; + } + + case Animation::TYPE_METHOD: + case Animation::TYPE_ANIMATION: { + // Nothing to do here. + } break; + } + return nullptr; +} diff --git a/scene/animation/animation_mixer.h b/scene/animation/animation_mixer.h index 247462b8ad..9dc48e7b1c 100644 --- a/scene/animation/animation_mixer.h +++ b/scene/animation/animation_mixer.h @@ -388,14 +388,13 @@ class AnimatedValuesBackup : public RefCounted { HashMap<NodePath, AnimationMixer::TrackCache *> data; public: - void set_data(const HashMap<NodePath, AnimationMixer::TrackCache *> p_data) { data = p_data; }; - HashMap<NodePath, AnimationMixer::TrackCache *> get_data() const { return data; }; + void set_data(const HashMap<NodePath, AnimationMixer::TrackCache *> p_data); + HashMap<NodePath, AnimationMixer::TrackCache *> get_data() const; + void clear_data(); - ~AnimatedValuesBackup() { - for (KeyValue<NodePath, AnimationMixer::TrackCache *> &K : data) { - memdelete(K.value); - } - } + AnimationMixer::TrackCache *get_cache_copy(AnimationMixer::TrackCache *p_cache) const; + + ~AnimatedValuesBackup() { clear_data(); } }; VARIANT_ENUM_CAST(AnimationMixer::AnimationCallbackModeProcess); |