Skip to content

GDScript: Clear base script in GDScript::clear#117991

Open
NoctemCat wants to merge 1 commit into
godotengine:masterfrom
NoctemCat:clear_base_before_next
Open

GDScript: Clear base script in GDScript::clear#117991
NoctemCat wants to merge 1 commit into
godotengine:masterfrom
NoctemCat:clear_base_before_next

Conversation

@NoctemCat
Copy link
Copy Markdown
Contributor

@NoctemCat NoctemCat commented Mar 30, 2026

Fixes: #117975

We can access an already freed script when clearing dependencies between scripts. It can happen because sometimes the reference to the base script can be the last reference and at the same time the next one in the script_list. So we first advance list to the next node and then free the base script in the Ref<GDScript> scr destructor.

To fix it I choose to manually clear the base script before moving to the next node to allow it to remove itself from the script_list if it needs to.

As recommended in Rocket chat the code to clean it was moved to GDScript::clear. It was still tested with tsan build and it also fixes it.

Tbh, not sure how often it happens, but it does happen in my project and in the mrp at least.

@NoctemCat NoctemCat requested a review from a team as a code owner March 30, 2026 06:43
@Nintorch Nintorch added this to the 4.x milestone Mar 30, 2026
@NoctemCat NoctemCat force-pushed the clear_base_before_next branch from 1e93251 to 0388291 Compare April 27, 2026 15:10
@NoctemCat NoctemCat changed the title GDScript: Fix inconsistent heap-use-after-free on exit GDScript: Clear base script in GDScript::clear Apr 27, 2026
Copy link
Copy Markdown
Member

@HolonProduction HolonProduction left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can confirm that this fixes the issue. The approach makes sense to me 👍

@HolonProduction HolonProduction modified the milestones: 4.x, 4.8 Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rare heap-use-after-free when clearing dependencies between scripts during exit

3 participants