Skip to content

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

Merged
Repiteo merged 1 commit into
godotengine:masterfrom
NoctemCat:clear_base_before_next
Jun 18, 2026
Merged

GDScript: Clear base script in GDScript::clear#117991
Repiteo merged 1 commit into
godotengine:masterfrom
NoctemCat:clear_base_before_next

Conversation

@NoctemCat

@NoctemCat NoctemCat commented Mar 30, 2026

Copy link
Copy Markdown
Contributor

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

@HolonProduction HolonProduction left a comment

Copy link
Copy Markdown
Member

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
@Repiteo
Repiteo merged commit 0dd6299 into godotengine:master Jun 18, 2026
20 checks passed
@Repiteo

Repiteo commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Thanks!

@NoctemCat
NoctemCat deleted the clear_base_before_next branch June 19, 2026 01:28
BendyLand pushed a commit to BendyLand/voltaire that referenced this pull request Aug 2, 2026
…e_next

GDScript: Clear base script in `GDScript::clear`
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

4 participants