Skip to content

Fix crash when creating actors from OnStart - #851

Merged
adriengivry merged 2 commits into
Overload-Technologies:mainfrom
Paaanaaamaaa:fix/actor-loading-crash-844
Sep 19, 2026
Merged

adriengivry merged 2 commits into
Overload-Technologies:mainfrom
Paaanaaamaaa:fix/actor-loading-crash-844

Conversation

@Paaanaaamaaa

@Paaanaaamaaa Paaanaaamaaa commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes a crash when entering play mode caused by creating actors from OnAwake, OnEnable or OnStart.

Scene::Play iterated over m_actors directly while invoking these callbacks. If a script called Scene::CreateActor from one of them, m_actors could be reallocated mid-loop, invalidating the iterators and causing an access violation in Actor::IsActive. It only crashed when the actor holding the script wasn't the last one in the scene, because the loop stopped right after the last actor.

Scene::Play now iterates over a copy of m_actors, the same way Update, FixedUpdate and LateUpdate already do. The copy is taken once before all three callbacks so actors created during OnAwake don't get OnEnable/OnStart called twice.

While fixing this, the BeginBatchActorCreation/EndBatchActorCreation Play's callbacks was removed. CreateActor already starts actors created during play immediately, so the batch was redundant, and it caused a nested-batch assert when a callback called Scene::InstantiatePrefab. EndBatchActorCreation is reverted to its previous shape: it swaps the pending actors into a local list and clears m_batchActorCreation before running any callback, which removes the nesting problem and makes the while loop unnecessary.

Related Issue(s)

Fixes #844

Review Guidance

The only functional change is in Scene::Play

Screenshots/GIFs

None!

AI Usage Disclosure

None!

Checklist

  • My code follows the project's code style guidelines
  • When applicable, I have commented my code, particularly in hard-to-understand areas
  • When applicable, I have updated the documentation accordingly
  • My changes don't generate new warnings or errors
  • I have reviewed and take responsibility for all code in this PR (including any AI-assisted contributions)

Comment thread Sources/OvCore/src/OvCore/SceneSystem/Scene.cpp Outdated
Comment thread Sources/OvCore/src/OvCore/SceneSystem/Scene.cpp Outdated

@Gopmyc Gopmyc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

NIT : the PR title ends up as the squashed commit message, and the crash happens when entering play mode rather than during loading. Maybe something closer to the issue title, like "Fix crash when creating actors from OnStart" ?

@Paaanaaamaaa Paaanaaamaaa changed the title Fix actor instantiation crash during loading phase (#844) Fix crash when creating actors from OnStart Sep 15, 2026
@Paaanaaamaaa
Paaanaaamaaa force-pushed the fix/actor-loading-crash-844 branch from dda31a6 to 3b4914c Compare September 15, 2026 02:36

@adriengivry adriengivry 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.

Thanks for contributing! 🎉

Please use the standard PR template https://github.com/Overload-Technologies/Overload/blob/main/.github/pull_request_template.md

Scene::Play iterated over m_actors directly while invoking OnAwake, OnEnable and OnStart. If a script called Scene::CreateActor from one of these callbacks, m_actors could be reallocated mid-loop, invalidating the iterators and causing an access violation in Actor::IsActive. Iterating over a copy of m_actors, as Update/FixedUpdate/LateUpdate already do, fixes the crash.

While fixing this, the actor creation batch that Play wrapped around its callbacks was also removed. CreateActor already runs OnAwake/OnEnable/OnStart immediately on actors created during play, so the batch was redundant. It also introduced a bug: EndBatchActorCreation ran those same callbacks while m_batchActorCreation was still true, so a callback that called Scene::InstantiatePrefab would hit the 'Cannot begin an actor creation batch while another one is still active' assert in Debug, or silently clear m_batchCreatedActors in Release, leaving actors created earlier in the same callback unstarted.

EndBatchActorCreation is reverted to swap the pending actors into a local list and reset m_batchActorCreation before invoking any callback, which removes the nesting problem and makes the while loop unnecessary.

Fixes Overload-Technologies#844
@Paaanaaamaaa
Paaanaaamaaa force-pushed the fix/actor-loading-crash-844 branch from 3b4914c to 6ce805d Compare September 16, 2026 02:18
@Paaanaaamaaa
Paaanaaamaaa requested a review from Gopmyc September 16, 2026 02:19

@Gopmyc Gopmyc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM ! ✅

@adriengivry
adriengivry merged commit 51b3767 into Overload-Technologies:main Sep 19, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Crash when creating actors from OnStart

3 participants