From e815a8f899d94422c2026b0146fdd7dde5a5a28f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pa=CC=84vels=20Nadtoc=CC=8Cajevs?= <7645683+bruvzg@users.noreply.github.com> Date: Wed, 26 Nov 2025 09:59:07 +0200 Subject: [PATCH] Do not attempt deleting local cache in `Resource::_teardown_duplicate_from_variant`. --- core/io/resource.cpp | 13 +++++++++++-- core/io/resource.h | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/core/io/resource.cpp b/core/io/resource.cpp index 1bf4eecf24f..50d399db717 100644 --- a/core/io/resource.cpp +++ b/core/io/resource.cpp @@ -368,11 +368,14 @@ Ref Resource::_duplicate(const DuplicateParams &p_params) const { ERR_FAIL_COND_V_MSG(p_params.local_scene && p_params.subres_mode != RESOURCE_DEEP_DUPLICATE_MAX, Ref(), "Duplication for local-to-scene can't specify a deep duplicate mode."); DuplicateRemapCacheT *remap_cache_backup = thread_duplicate_remap_cache; + bool remap_cache_needs_deallocation_backup = thread_duplicate_remap_cache_needs_deallocation; // These are for avoiding potential duplicates that can happen in custom code // from participating in the same duplication session (remap cache). #define BEFORE_USER_CODE thread_duplicate_remap_cache = nullptr; -#define AFTER_USER_CODE thread_duplicate_remap_cache = remap_cache_backup; +#define AFTER_USER_CODE \ + thread_duplicate_remap_cache = remap_cache_backup; \ + thread_duplicate_remap_cache_needs_deallocation = remap_cache_needs_deallocation_backup; List plist; get_property_list(&plist); @@ -434,7 +437,9 @@ Ref Resource::duplicate_for_local_scene(Node *p_for_scene, DuplicateRe #endif DuplicateRemapCacheT *remap_cache_backup = thread_duplicate_remap_cache; + bool remap_cache_needs_deallocation_backup = thread_duplicate_remap_cache_needs_deallocation; thread_duplicate_remap_cache = &p_remap_cache; + thread_duplicate_remap_cache_needs_deallocation = false; DuplicateParams params; params.deep = true; @@ -442,6 +447,7 @@ Ref Resource::duplicate_for_local_scene(Node *p_for_scene, DuplicateRe const Ref &dupe = _duplicate(params); thread_duplicate_remap_cache = remap_cache_backup; + thread_duplicate_remap_cache_needs_deallocation = remap_cache_needs_deallocation_backup; return dupe; } @@ -504,6 +510,7 @@ Ref Resource::duplicate(bool p_deep) const { bool started_session = false; if (!thread_duplicate_remap_cache) { thread_duplicate_remap_cache = &remap_cache; + thread_duplicate_remap_cache_needs_deallocation = false; started_session = true; } @@ -526,6 +533,7 @@ Ref Resource::duplicate_deep(ResourceDeepDuplicateMode p_deep_subresou bool started_session = false; if (!thread_duplicate_remap_cache) { thread_duplicate_remap_cache = &remap_cache; + thread_duplicate_remap_cache_needs_deallocation = false; started_session = true; } @@ -571,6 +579,7 @@ Ref Resource::_duplicate_from_variant(bool p_deep, ResourceDeepDuplica } } else { thread_duplicate_remap_cache = memnew(DuplicateRemapCacheT); + thread_duplicate_remap_cache_needs_deallocation = true; } DuplicateParams params; @@ -583,7 +592,7 @@ Ref Resource::_duplicate_from_variant(bool p_deep, ResourceDeepDuplica } void Resource::_teardown_duplicate_from_variant() { - if (thread_duplicate_remap_cache) { + if (thread_duplicate_remap_cache && thread_duplicate_remap_cache_needs_deallocation) { memdelete(thread_duplicate_remap_cache); thread_duplicate_remap_cache = nullptr; } diff --git a/core/io/resource.h b/core/io/resource.h index 480bde28e84..7feffb7458f 100644 --- a/core/io/resource.h +++ b/core/io/resource.h @@ -94,6 +94,7 @@ private: using DuplicateRemapCacheT = HashMap, Ref>; static thread_local inline DuplicateRemapCacheT *thread_duplicate_remap_cache = nullptr; + static thread_local inline bool thread_duplicate_remap_cache_needs_deallocation = true; Variant _duplicate_recursive(const Variant &p_variant, const DuplicateParams &p_params, uint32_t p_usage = 0) const; void _find_sub_resources(const Variant &p_variant, HashSet> &p_resources_found);