Skip to content

Commit 9ea58b0

Browse files
committed
Added checks to ensure BitReader is only disposed once
1 parent bf29d19 commit 9ea58b0

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

MLAPI/NetworkingManagerComponents/Binary/BitReader.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace MLAPI.NetworkingManagerComponents.Binary
99
{
1010
public class BitReader : IDisposable
1111
{
12+
private bool disposed;
1213
private delegate T Getter<T>();
1314
private static readonly float[] holder_f = new float[1];
1415
private static readonly double[] holder_d = new double[1];
@@ -24,6 +25,7 @@ public class BitReader : IDisposable
2425
private BitReader(byte[] readFrom)
2526
{
2627
this.readFrom = readFrom;
28+
disposed = false;
2729
}
2830

2931
public static BitReader Get(byte[] readFrom)
@@ -33,12 +35,14 @@ public static BitReader Get(byte[] readFrom)
3335
if (pools > 10)
3436
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("There are more than 10 BitReaders. Have you forgotten do dispose? (More readers hurt performance)");
3537
BitReader reader = new BitReader(readFrom);
38+
reader.disposed = false;
3639
pools++;
3740
return reader;
3841
}
3942
else
4043
{
4144
BitReader reader = readerPool.Dequeue();
45+
reader.disposed = false;
4246
reader.readFrom = readFrom;
4347
return reader;
4448
}
@@ -173,6 +177,8 @@ public void Dispose()
173177
{
174178
readFrom = null; //Give to GC
175179
bitCount = 0;
180+
if (disposed) return; //this is already in the pool
181+
disposed = true;
176182
readerPool.Enqueue(this);
177183
}
178184
}

0 commit comments

Comments
 (0)