Skip to content

Commit 7d26478

Browse files
committed
fix: Fixed an issue that caused overflown memory to not be reset before being provided to the user
Special thanks to @jaglitegrann for finding and assisting with this issue
1 parent d06f6f8 commit 7d26478

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

MLAPI/Serialization/Pooled/BitStreamPool.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ public static PooledBitStream GetStream()
2828

2929
if (weakStream != null)
3030
{
31-
return (PooledBitStream)weakStream;
31+
PooledBitStream strongStream = (PooledBitStream)weakStream;
32+
33+
strongStream.SetLength(0);
34+
strongStream.Position = 0;
35+
36+
return strongStream;
3237
}
3338
}
3439

@@ -44,6 +49,7 @@ public static PooledBitStream GetStream()
4449
}
4550

4651
PooledBitStream stream = streams.Dequeue();
52+
4753
stream.SetLength(0);
4854
stream.Position = 0;
4955

@@ -58,10 +64,10 @@ public static void PutBackInPool(PooledBitStream stream)
5864
{
5965
if (streams.Count > 16)
6066
{
61-
//The user just created lots of streams without returning them in between.
62-
//Streams are essentially byte array wrappers. This is valuable memory.
63-
//Thus we put this stream as a weak reference incase of another burst
64-
//But still leave it to GC
67+
// The user just created lots of streams without returning them in between.
68+
// Streams are essentially byte array wrappers. This is valuable memory.
69+
// Thus we put this stream as a weak reference incase of another burst
70+
// But still leave it to GC
6571
if (LogHelper.CurrentLogLevel <= LogLevel.Developer) LogHelper.LogInfo("Putting PooledBitStream into overflow pool. Did you forget to dispose?");
6672
overflowStreams.Enqueue(new WeakReference(stream));
6773
}

0 commit comments

Comments
 (0)