Use after free and resource leakage during shutdown
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Needs clarification
- Activity status
- Quiet
- Tech stack
- cpp
- Domain
- computer-graphics
Research direction
Start by reproducing the shutdown failure with the reported custom canvas items, get_children(), and godot::cast_to<> under ASan/UBSan. Trace RendererCanvasRenderRD::_canvas_texture_invalidation_callback() in servers/rendering/renderer_rd/renderer_canvas_render_rd.cpp alongside CanvasTexture::~CanvasTexture() in servers/rendering/renderer_rd/storage_rd/texture_storage.cpp; done means shutdown completes without the reported use-after-free or resource-leak diagnostics.
Written by the indexing model from the issue text.
Description
Godot version
4.5.stable
godot-cpp version
godot-4.5-stable
System information
Linux (Debian 13.1)
Issue description
I am using gdextension to work with customized physics code.
We observe every run two problematic symptoms:
- engine always complains during the shutdown about the resource leakage
- it oftens exits "with crash"
I compiled godot from source code with ubsan and asan to have a better understanding of what's going on.
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior thirdparty/harfbuzz/src/hb-buffer.hh:479:69
ERROR: 192 RID allocations of type 'P12GodotShape2D' were leaked at exit.
WARNING: 17 RIDs of type "CanvasItem" were leaked.
at: _free_rids (servers/rendering/renderer_canvas_cull.cpp:2678)
ERROR: 16 RID allocations of type 'N10RendererRD14TextureStorage7TextureE' were leaked at exit.
ERROR: 6 RID allocations of type 'N10RendererRD14TextureStorage13CanvasTextureE' were leaked at exit.
=================================================================
==18433==ERROR: AddressSanitizer: heap-use-after-free on address 0x50600095afb0 at pc 0x558ab0ad19b6 bp 0x7ffe244eafa0 sp 0x7ffe244eaf98
READ of size 8 at 0x50600095afb0 thread T0
#0 0x558ab0ad19b5 in LocalVector<RID, unsigned int, false, true>::begin() /home/alice/soft/godot/./core/templates/local_vector.h:254:19
#1 0x558ab0ad19b5 in RendererCanvasRenderRD::_canvas_texture_invalidation_callback(bool, void*) /home/alice/soft/godot/servers/rendering/renderer_rd/renderer_canvas_render_rd.cpp:2973:15
#2 0x558aaff14423 in RendererRD::TextureStorage::CanvasTexture::~CanvasTexture() /home/alice/soft/godot/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp:53:3
#3 0x558aaff14423 in RID_Alloc<RendererRD::TextureStorage::CanvasTexture, true>::~RID_Alloc() /home/alice/soft/godot/./core/templates/rid_owner.h:449:65
#4 0x558aafe8b199 in RID_Owner<RendererRD::TextureStorage::CanvasTexture, true>::~RID_Owner() /home/alice/soft/godot/./core/templates/rid_owner.h:527:7
#5 0x558aafe8b199 in RendererRD::TextureStorage::~TextureStorage() /home/alice/soft/godot/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp:564:1
#6 0x558aaf585354 in void memdelete<RendererRD::TextureStorage>(RendererRD::TextureStorage*) /home/alice/soft/godot/./core/os/memory.h:139:13
#7 0x558aaf585354 in RendererCompositorRD::finalize() /home/alice/soft/godot/servers/rendering/renderer_rd/renderer_compositor_rd.cpp:179:2
#8 0x558aaf18c02d in RenderingServerDefault::_finish() /home/alice/soft/godot/servers/rendering/rendering_server_default.cpp:245:19
#9 0x558a9ce77829 in finalize_display() /home/alice/soft/godot/main/main.cpp:404:20
#10 0x558a9cf0e4df in Main::cleanup(bool) /home/alice/soft/godot/main/main.cpp:5061:2
#11 0x558a9cc1f3a7 in main /home/alice/soft/godot/platform/linuxbsd/godot_linuxbsd.cpp:89:2
#12 0x7f9589c49ca7 (/lib/x86_64-linux-gnu/libc.so.6+0x29ca7) (BuildId: c495b62edadd6c356265942ec1282d98058a7b41)
#13 0x7f9589c49d64 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29d64) (BuildId: c495b62edadd6c356265942ec1282d98058a7b41)
#14 0x558a9cb3b690 in _start (/home/alice/soft/godot/bin/godot.linuxbsd.editor.x86_64.llvm.san+0xab8d690) (BuildId: d04aa1bb3047432769e682d01b15f0456528d423)
0x50600095afb0 is located 48 bytes inside of 56-byte region [0x50600095af80,0x50600095afb8)
freed by thread T0 here:
#0 0x558a9cbdb22a in free (/home/alice/soft/godot/bin/godot.linuxbsd.editor.x86_64.llvm.san+0xac2d22a) (BuildId: d04aa1bb3047432769e682d01b15f0456528d423)
#1 0x558ab0aea6b7 in void memdelete<HashMapElement<RID, LocalVector<RID, unsigned int, false, true>>>(HashMapElement<RID, LocalVector<RID, unsigned int, false, true>>*) /home/alice/soft/godot/./core/os/memory.h:142:2
#2 0x558ab0aea6b7 in DefaultTypedAllocator<HashMapElement<RID, LocalVector<RID, unsigned int, false, true>>>::delete_allocation(HashMapElement<RID, LocalVector<RID, unsigned int, false, true>>*) /home/alice/soft/godot/./core/os/memory.h:258:59
#3 0x558ab0aea6b7 in HashMap<RID, LocalVector<RID, unsigned int, false, true>, HashMapHasherDefault, HashMapComparatorDefault<RID>, DefaultTypedAllocator<HashMapElement<RID, LocalVector<RID, unsigned int, false, true>>>>::clear() /home/alice/soft/godot/./core/templates/hash_map.h:257:15
previously allocated by thread T0 here:
#0 0x558a9cbdb4c3 in malloc (/home/alice/soft/godot/bin/godot.linuxbsd.editor.x86_64.llvm.san+0xac2d4c3) (BuildId: d04aa1bb3047432769e682d01b15f0456528d423)
#1 0x558ab120a91b in void* Memory::alloc_static<false>(unsigned long, bool) /home/alice/soft/godot/core/os/memory.cpp:108:9
#2 0x000100000000 (<unknown module>)
SUMMARY: AddressSanitizer: heap-use-after-free /home/alice/soft/godot/./core/templates/local_vector.h:254:19 in LocalVector<RID, unsigned int, false, true>::begin()
Shadow bytes around the buggy address:
Steps to reproduce
Combination of:
- Creating custom canvas items inherited from tilemap or node2d
- inspecting tree state using get_children() and godot::cast_to<>.
I have a suspicion godot::cast_to is problematic because it creates a wrapping reference to the object which I couldn't find where it is getting deleted.
Minimal reproduction project
Sorry, crunch time right now, can prepare one later if requested.
- Dominant language
- C++
- Stars
- 2.7k
- Forks
- 810
- Avg merge
- 1d 3h
- Merged PRs (30d)
- 8
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from godotengine/godot-cpp
-
enhancement topic:buildsystem
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
godotengine/godot-cpp#2002 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
godotengine/godot-cpp#1969 · 4 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
godotengine/godot-cpp#1967 ·
-
bug
Difficulty 3/5 1-2 days Newbie friendliness 50/100
godotengine/godot-cpp#2042 ·
-
bug crash
Difficulty 4/5 3-5 days Newbie friendliness 35/100
godotengine/godot-cpp#2024 · 5 comments ·
All issues in godotengine/godot-cpp
Similar issues
-
enhancement
Difficulty 1/5 Under an hour Newbie friendliness 88/100
QuantStack/git2cpp#187 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
mlcommons/mobile_app_open#1182 ·
-
Needs-Triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
microsoft/winget-cli#6547 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
AXERA-TECH/ax-llm#77 ·