Skip to content

Commit 6e5b210

Browse files
author
Oren (electricessence)
committed
Added some disposed validation.
1 parent b6b314a commit 6e5b210

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

source/ObjectPoolAutoTrimmer.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ public ObjectPoolAutoTrimmer(
3434
TimeSpan? trimDelay = null)
3535
{
3636
_pool = pool ?? throw new ArgumentNullException("pool");
37-
if (pool is DisposableBase d)
38-
d.BeforeDispose += Pool_BeforeDispose;
3937

4038
TrimmedSize = _trimmedSize = trimmedSize;
4139
TrimDelay = _trimDelay = trimDelay ?? TimeSpan.FromMilliseconds(500);
@@ -45,6 +43,11 @@ public ObjectPoolAutoTrimmer(
4543
pool.GivenTo += Target_GivenTo;
4644
pool.TakenFrom += Target_TakenFrom;
4745

46+
if (pool is DisposableBase d)
47+
{
48+
if (d.IsDisposed) throw new ArgumentException("Cannot trim for an object pool that is already disposed.");
49+
d.BeforeDispose += Pool_BeforeDispose;
50+
}
4851
}
4952

5053
protected virtual void Target_GivenTo(int newSize)

source/Recycler.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ public Recycler(
3232
});
3333

3434
if (target is DisposableBase d)
35+
{
36+
if (d.IsDisposed) throw new ArgumentException("Cannot recycle for an object pool that is already disposed.");
3537
d.BeforeDispose += Pool_BeforeDispose;
38+
// Could possibly dispose before this line somewhere... But that's just nasty. :P
39+
}
3640
}
3741

3842
public Recycler(

0 commit comments

Comments
 (0)