Skip to content

Commit

Permalink
Merge pull request #9 from wlindley/additional-exceptions
Browse files Browse the repository at this point in the history
Adding exceptions to try to uncover root cause of rare NullRefException we're seeing in the logs.
  • Loading branch information
wlindley authored Jun 7, 2017
2 parents 29c39e4 + 6fc17bb commit 3ffafc6
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion Bantam/ObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,14 @@ private void ValidateType(Type type)
UniqueInstances[type]++;
}
else
instance = instances[type].Dequeue() as T;
{
var poolable = instances[type].Dequeue();
if (null == poolable)
throw new NullInPoolException1();
if (!(poolable is T))
throw new MismatchedTypeInPoolException1();
instance = poolable as T;
}
instance.Reset();
return instance;
}
Expand All @@ -127,9 +134,19 @@ private Poolable GetInstance(Type type)
{
instance = Activator.CreateInstance(type) as Poolable;
UniqueInstances[type]++;
if (null == instance)
throw new NullInPoolException2();
if (!type.IsInstanceOfType(instance))
throw new MismatchedTypeInPoolException2();
}
else
{
instance = instances[type].Dequeue();
if (null == instance)
throw new NullInPoolException3();
if (!type.IsInstanceOfType(instance))
throw new MismatchedTypeInPoolException3();
}
instance.Reset();
return instance;
}
Expand Down Expand Up @@ -157,4 +174,10 @@ public class ObjectPoolException : Exception {}
public class InvalidTypeException : ObjectPoolException {}
public class NullInstanceException : ObjectPoolException {}
public class MismatchedTypeException : ObjectPoolException {}
public class NullInPoolException1 : ObjectPoolException {}
public class MismatchedTypeInPoolException1 : ObjectPoolException {}
public class NullInPoolException2 : ObjectPoolException { }
public class MismatchedTypeInPoolException2 : ObjectPoolException { }
public class NullInPoolException3 : ObjectPoolException { }
public class MismatchedTypeInPoolException3 : ObjectPoolException { }
}

0 comments on commit 3ffafc6

Please sign in to comment.