Skip to content

fix: ServerDestroySpawnedSceneObjects throws an exception before switching scene #618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 15, 2021

Conversation

NoelStephensUnity
Copy link
Collaborator

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.
internal static void ServerDestroySpawnedSceneObjects()
{
//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
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
Collaborator 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
Copy link
Contributor

@LukeStampfli LukeStampfli left a comment

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

Copy link
Contributor

@mattwalsh-unity mattwalsh-unity left a comment

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