/// <summary>Disables the pool and disposes of all objects that remain in the freeObjctStack.</summary> public void Shutdown() { lock (freeObjectStackMutex) { // disable the pool poolIsEnabled = false; if (freeObjectStack != null) { // remove and dispose of all objects in the freeObjectStack while (freeObjectStack.Count > 0) { IPoolableRefCountedObject <ObjectType> obj = freeObjectStack.Pop(); ObjectType objAsDerivedType = obj as ObjectType; if (obj != null && objAsDerivedType != null) { obj.DisposeOfSelf(ref objAsDerivedType); } } freeObjectStack = null; } freeObjectStackCapacity = 0; } }
/// <summary>Implementation method for ReleaseToPoolDelegate. Confirms that object implements IPoolableRefCountedObject and that it belongs to this pool.</summary> /// <remarks>This delegate method is only called immediately after the object has decremented the reference count to zero. As such the test for RefCount == 0 is not repeated here.</remarks> private void ImplementReleaseObjectToPoolDelegate(ref ObjectType objRef) { IPoolableRefCountedObject <ObjectType> item = objRef as IPoolableRefCountedObject <ObjectType>; if (item == null || item.ReleaseObjectToPoolDelegate != roReleaseObjectToPoolDelegate) { Asserts.TakeBreakpointAfterConditionCheckFailed("Pool.ReleaseObjectToPool failed: returned object is not of correct type or does not belong to this pool."); return; // caller will dispose of objRef as appropriate } lock (freeObjectStackMutex) { if (poolIsEnabled && freeObjectStack.Count < freeObjectStackCapacity) { objRef = null; freeObjectStack.Push(item); item = null; } // else - caller will dispose of objRef as appropriate } }