Skip to content

fix: networkscenemanager not releasing buffers from pool #1132

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 10 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,8 @@ public void Shutdown()

if (SceneManager != null)
{
// Let the NetworkSceneManager clean up its two SceneEvenData instances
SceneManager.Dispose();
SceneManager = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class SceneEvent
/// Main class for managing network scenes when <see cref="NetworkConfig.EnableSceneManagement"/> is enabled.
/// Uses the <see cref="MessageQueueContainer.MessageType.SceneEvent"/> message to communicate <see cref="SceneEventData"/> between the server and client(s)
/// </summary>
public class NetworkSceneManager
public class NetworkSceneManager : IDisposable
{
// Used to be able to turn re-synchronization off for future snapshot development purposes.
internal static bool DisableReSynchronization;
Expand Down Expand Up @@ -202,6 +202,17 @@ public class NetworkSceneManager

internal Scene DontDestroyOnLoadScene;

/// <summary>
/// Handle NetworkSeneManager clean up
/// </summary>
public void Dispose()
{
SceneEventData.Dispose();
SceneEventData = null;
ClientSynchEventData.Dispose();
ClientSynchEventData = null;
}

/// <summary>
/// Gets the scene name from full path to the scene
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ internal void OnRead(NetworkReader reader)
}
case SceneEventTypes.S2C_Load:
{
SetInternalBuffer();
// We store off the trailing in-scene placed serialized NetworkObject data to
// be processed once we are done loading.
InternalBuffer.Position = 0;
Expand Down Expand Up @@ -514,8 +515,8 @@ internal void OnRead(NetworkReader reader)
/// <param name="reader"></param>
internal void CopySceneSyncrhonizationData(NetworkReader reader)
{
SetInternalBuffer();
m_NetworkObjectsSync.Clear();

ScenesToSynchronize = new Queue<uint>(reader.ReadUIntArrayPacked());
SceneHandlesToSynchronize = new Queue<uint>(reader.ReadUIntArrayPacked());
InternalBuffer.Position = 0;
Expand Down Expand Up @@ -548,6 +549,7 @@ internal void DeserializeScenePlacedObjects()
// Deserialize the NetworkObject
NetworkObject.DeserializeSceneObject(InternalBuffer as NetworkBuffer, reader, m_NetworkManager);
}
ReleaseInternalBuffer();
}

/// <summary>
Expand Down Expand Up @@ -694,6 +696,7 @@ internal void SynchronizeSceneNetworkObjects(NetworkManager networkManager)
m_NetworkObjectsSync.Add(spawnedNetworkObject);
}
}
ReleaseInternalBuffer();
}

/// <summary>
Expand Down Expand Up @@ -737,9 +740,20 @@ internal void ReadSceneEventProgressDone(NetworkReader reader)
}

/// <summary>
/// Used to release the pooled network buffer
/// Gets a PooledNetworkBuffer if needed
/// </summary>
public void Dispose()
private void SetInternalBuffer()
{
if (InternalBuffer == null)
{
InternalBuffer = NetworkBufferPool.GetBuffer();
}
}

/// <summary>
/// Releases the PooledNetworkBuffer when no longer needed
/// </summary>
private void ReleaseInternalBuffer()
{
if (InternalBuffer != null)
{
Expand All @@ -749,12 +763,19 @@ public void Dispose()
}

/// <summary>
/// Constructor
/// Used to release the pooled network buffer
/// </summary>
public void Dispose()
{
ReleaseInternalBuffer();
}

/// <summary>
/// Constructor for SceneEventData
/// </summary>
internal SceneEventData(NetworkManager networkManager)
{
m_NetworkManager = networkManager;
InternalBuffer = NetworkBufferPool.GetBuffer();
}
}
}