Skip to content

fix: ServerDestroySpawnedSceneObjects throws an exception before switching scene - #618

Merged
mattwalsh-unity merged 4 commits into
release/0.1.0from
fix/serverdestroysceneobjects
Mar 15, 2021
Merged

fix: ServerDestroySpawnedSceneObjects throws an exception before switching scene#618
mattwalsh-unity merged 4 commits into
release/0.1.0from
fix/serverdestroysceneobjects

Conversation

@NoelStephensUnity

Copy link
Copy Markdown
Member

This is the fix for MTT-544 where transitioning from a scene that has InSceneObjects the Host-Server will throw an exception due to a list in the middle of an enumerative processing loop is modified.

The Jira Bug MTT-544 describes the steps to reproduce the exception and to verify this branch fixes those specific exceptions.

If you have in-scene placed NetworkObjects and you transition to a new scene then a crash will occur in NetworkSpawnManager.ServerDestroySpawnedSceneObjects due to NetworkObjects being removed from the SpawnedObjectsList during the "OnDestroy" invocation.  This fixes that crash.
Adding some comments as to why the code is added, camelCase for locals, and generic Var as opposed to specific type for declaration.
{
//This Allocation is "ok" for now because this code only executes when a new scene is switched to
//We need to keep track of GameObjects and NetworkObjects to be destroyed in the current scene
//but only destroy them once outside of the enumerative foreach loop

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.

Ok...would you also capture the 'why' here too in the comment?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Simplified the code required and adjusted the comment.

This applies a better approach to the issue of removing objects from a list that is being iterated over without throwing the "Collection was modified; enumeration operation may not execute" exception due to NetworkObjects removing themselves from the SpawnedObjectList during OnDestroy.
Minor tweak to the comment as to why the code was added.
@NoelStephensUnity
NoelStephensUnity marked this pull request as ready for review March 15, 2021 18:49

@LukeStampfli LukeStampfli 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 🚀

internal static void ServerDestroySpawnedSceneObjects()
{
foreach (var sobj in SpawnedObjectsList)
//This Allocation is "ok" for now because this code only executes when a new scene is switched to

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.

Is this comment still real? I don't see an allocation per se...or do you mean the ToList() call?

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.

Yes ToList creates a copy of the collection and allocates a new list object in the process.

@mattwalsh-unity mattwalsh-unity 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.

Nice fix!

@mattwalsh-unity
mattwalsh-unity merged commit 7346a2f into release/0.1.0 Mar 15, 2021
@mattwalsh-unity
mattwalsh-unity deleted the fix/serverdestroysceneobjects branch March 15, 2021 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants