Fix crash when detaching an element from a colshape hit event - #5185
Merged
Conversation
DoAttaching moves the attached element; for a colshape that fires hit and leave events, so a handler can detach the element in the middle of the update. The lighting sync added in multitheftauto#4984 then used the attach target again without rechecking it and crashed on the null pointer. Fixes multitheftauto#5164
FileEX
approved these changes
Aug 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
DoAttaching moves the attached element to follow whatever it is attached to. Moving a colshape fires its hit and leave events right there, so script handlers run while DoAttaching is still in the middle of the update. If one of those handlers detaches the element, m_pAttachedToEntity turns null while the function is still using it; the lighting sync added in #4984 reads that pointer again after the move, so it crashed on the null pointer. The attachment is now checked again after moving, and the update simply stops there if the element got detached.
Motivation
Fixes #5164.
Two hours to reproduce the crash and 2 minutes to fix it.Test plan
Reproduced it with a small clientside resource: create a ped with a colsphere attached to it, add an onClientColShapeHit handler that calls detachElements when the local player enters, then teleport the ped onto the local player. DoAttaching drags the colshape onto the player, the hit event fires and the handler detaches it; without this change the client crashes right after the handler returns, with this change the detach just works and the game carries on. Also checked that attached objects still take the lighting of the entity they are attached to, so the #4984 behaviour stays intact when nothing detaches.
repro.zip
Checklist