-
Notifications
You must be signed in to change notification settings - Fork 450
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
Conversation
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice fix!
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.