Skip to content

Commit 8a74421

Browse files
fix: networkscenemanager not releasing buffers from pool (#1132)
* fix Don't used pooled NetworkBuffers for SceneEventData, instead just allocate 1 dedicated NetworkBuffer per SceneEventData (which will yield 2 instances per NetworkSceneManager). Make sure we dispose the scene manager so it will dispose the two SceneEventData instances so they will Dispose their NetworkBuffers. * style Adding comments * refactor Switching back to PooledNetworkBuffer. Now it only gets a PooledNetworkBuffer when needed, and releases it back into the pool when it is no longer needed. * style Fixing one standards violation introduced in this PR and several more that were introduced when metrics were added. * style and refactor Removed a LF that was not intended. Added comments to SetInternalBuffer and ReleaseInternalBuffer while also making them both private as nothing else needs to be calling these two methods. * refactor Removing standards check. * style Removing 1 LF from SceneEventData constructor. Slightly improved constructor description. Removed 1 LF between ReleaseInternalBuffer and SetInternalBuffer Removed 1 LF between AddSceneToSynchronize and SceneHandlesToSynchronize property.
1 parent f1a0706 commit 8a74421

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,8 @@ public void Shutdown()
951951

952952
if (SceneManager != null)
953953
{
954+
// Let the NetworkSceneManager clean up its two SceneEvenData instances
955+
SceneManager.Dispose();
954956
SceneManager = null;
955957
}
956958

com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public class SceneEvent
8585
/// Main class for managing network scenes when <see cref="NetworkConfig.EnableSceneManagement"/> is enabled.
8686
/// Uses the <see cref="MessageQueueContainer.MessageType.SceneEvent"/> message to communicate <see cref="SceneEventData"/> between the server and client(s)
8787
/// </summary>
88-
public class NetworkSceneManager
88+
public class NetworkSceneManager : IDisposable
8989
{
9090
// Used to be able to turn re-synchronization off for future snapshot development purposes.
9191
internal static bool DisableReSynchronization;
@@ -202,6 +202,17 @@ public class NetworkSceneManager
202202

203203
internal Scene DontDestroyOnLoadScene;
204204

205+
/// <summary>
206+
/// Handle NetworkSeneManager clean up
207+
/// </summary>
208+
public void Dispose()
209+
{
210+
SceneEventData.Dispose();
211+
SceneEventData = null;
212+
ClientSynchEventData.Dispose();
213+
ClientSynchEventData = null;
214+
}
215+
205216
/// <summary>
206217
/// Gets the scene name from full path to the scene
207218
/// </summary>

com.unity.netcode.gameobjects/Runtime/SceneManagement/SceneEventData.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ internal void OnRead(NetworkReader reader)
485485
}
486486
case SceneEventTypes.S2C_Load:
487487
{
488+
SetInternalBuffer();
488489
// We store off the trailing in-scene placed serialized NetworkObject data to
489490
// be processed once we are done loading.
490491
InternalBuffer.Position = 0;
@@ -514,8 +515,8 @@ internal void OnRead(NetworkReader reader)
514515
/// <param name="reader"></param>
515516
internal void CopySceneSyncrhonizationData(NetworkReader reader)
516517
{
518+
SetInternalBuffer();
517519
m_NetworkObjectsSync.Clear();
518-
519520
ScenesToSynchronize = new Queue<uint>(reader.ReadUIntArrayPacked());
520521
SceneHandlesToSynchronize = new Queue<uint>(reader.ReadUIntArrayPacked());
521522
InternalBuffer.Position = 0;
@@ -548,6 +549,7 @@ internal void DeserializeScenePlacedObjects()
548549
// Deserialize the NetworkObject
549550
NetworkObject.DeserializeSceneObject(InternalBuffer as NetworkBuffer, reader, m_NetworkManager);
550551
}
552+
ReleaseInternalBuffer();
551553
}
552554

553555
/// <summary>
@@ -694,6 +696,7 @@ internal void SynchronizeSceneNetworkObjects(NetworkManager networkManager)
694696
m_NetworkObjectsSync.Add(spawnedNetworkObject);
695697
}
696698
}
699+
ReleaseInternalBuffer();
697700
}
698701

699702
/// <summary>
@@ -737,9 +740,20 @@ internal void ReadSceneEventProgressDone(NetworkReader reader)
737740
}
738741

739742
/// <summary>
740-
/// Used to release the pooled network buffer
743+
/// Gets a PooledNetworkBuffer if needed
741744
/// </summary>
742-
public void Dispose()
745+
private void SetInternalBuffer()
746+
{
747+
if (InternalBuffer == null)
748+
{
749+
InternalBuffer = NetworkBufferPool.GetBuffer();
750+
}
751+
}
752+
753+
/// <summary>
754+
/// Releases the PooledNetworkBuffer when no longer needed
755+
/// </summary>
756+
private void ReleaseInternalBuffer()
743757
{
744758
if (InternalBuffer != null)
745759
{
@@ -749,12 +763,19 @@ public void Dispose()
749763
}
750764

751765
/// <summary>
752-
/// Constructor
766+
/// Used to release the pooled network buffer
767+
/// </summary>
768+
public void Dispose()
769+
{
770+
ReleaseInternalBuffer();
771+
}
772+
773+
/// <summary>
774+
/// Constructor for SceneEventData
753775
/// </summary>
754776
internal SceneEventData(NetworkManager networkManager)
755777
{
756778
m_NetworkManager = networkManager;
757-
InternalBuffer = NetworkBufferPool.GetBuffer();
758779
}
759780
}
760781
}

0 commit comments

Comments
 (0)