Skip to content

Commit 2e61399

Browse files
committed
Added double dispose checks to pooled Bit-stream, writer and reader
1 parent 6d5a6df commit 2e61399

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

MLAPI/NetworkingManagerComponents/Binary/ResourcePool.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,21 +150,28 @@ public static void PutBackInPool(PooledBitReader reader)
150150
/// </summary>
151151
public sealed class PooledBitStream : BitStream, IDisposable
152152
{
153+
private bool isDisposed = false;
153154
/// <summary>
154155
/// Gets a PooledBitStream from the static BitStreamPool
155156
/// </summary>
156157
/// <returns>PooledBitStream</returns>
157158
public static PooledBitStream Get()
158159
{
159-
return BitStreamPool.GetStream();
160+
PooledBitStream stream = BitStreamPool.GetStream();
161+
stream.isDisposed = false;
162+
return stream;
160163
}
161164

162165
/// <summary>
163166
/// Returns the PooledBitStream into the static BitStreamPool
164167
/// </summary>
165168
public new void Dispose()
166169
{
167-
BitStreamPool.PutBackInPool(this);
170+
if (!isDisposed)
171+
{
172+
isDisposed = true;
173+
BitStreamPool.PutBackInPool(this);
174+
}
168175
}
169176
}
170177

@@ -173,6 +180,8 @@ public static PooledBitStream Get()
173180
/// </summary>
174181
public sealed class PooledBitWriter : BitWriter, IDisposable
175182
{
183+
private bool isDisposed = false;
184+
176185
public PooledBitWriter(Stream stream) : base(stream)
177186
{
178187

@@ -184,15 +193,21 @@ public PooledBitWriter(Stream stream) : base(stream)
184193
/// <returns>PooledBitWriter</returns>
185194
public static PooledBitWriter Get(Stream stream)
186195
{
187-
return BitWriterPool.GetWriter(stream);
196+
PooledBitWriter writer = BitWriterPool.GetWriter(stream);
197+
writer.isDisposed = false;
198+
return writer;
188199
}
189200

190201
/// <summary>
191202
/// Returns the PooledBitWriter into the static BitWriterPool
192203
/// </summary>
193204
public void Dispose()
194205
{
195-
BitWriterPool.PutBackInPool(this);
206+
if (!isDisposed)
207+
{
208+
isDisposed = true;
209+
BitWriterPool.PutBackInPool(this);
210+
}
196211
}
197212
}
198213

@@ -201,6 +216,8 @@ public void Dispose()
201216
/// </summary>
202217
public sealed class PooledBitReader : BitReader, IDisposable
203218
{
219+
private bool isDisposed = false;
220+
204221
public PooledBitReader(Stream stream) : base(stream)
205222
{
206223
}
@@ -211,15 +228,21 @@ public PooledBitReader(Stream stream) : base(stream)
211228
/// <returns>PooledBitReader</returns>
212229
public static PooledBitReader Get(Stream stream)
213230
{
214-
return BitReaderPool.GetReader(stream);
231+
PooledBitReader reader = BitReaderPool.GetReader(stream);
232+
reader.isDisposed = false;
233+
return reader;
215234
}
216235

217236
/// <summary>
218237
/// Returns the PooledBitReader into the static BitReaderPool
219238
/// </summary>
220239
public void Dispose()
221240
{
222-
BitReaderPool.PutBackInPool(this);
241+
if (!isDisposed)
242+
{
243+
isDisposed = true;
244+
BitReaderPool.PutBackInPool(this);
245+
}
223246
}
224247
}
225248
}

0 commit comments

Comments
 (0)