In my class I have unmanaged objects in managed collection (pendingFramesBuffer). I need to implemented IDisposable interface.
My question: Where I need to clear pendingFramesBuffer collection? In the if (disposing) {...} condition or not.
public class Example : IDisposable
{
// unmanaged pointer in managed collection
private BlockingCollection<IntPtr> pendingFramesBuffer = new BlockingCollection<IntPtr>();
/// SOME CODE TO FILL pendingFramesBuffer COLLECTION
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
}
// CLEAR pendingFramesBuffer COLLECTION
while (pendingFramesBuffer != null && pendingFramesBuffer.Count > 1)
{
var tookFrameBuffer = pendingFramesBuffer.Take();
Marshal.FreeHGlobal(tookFrameBuffer);
}
this.disposed = true;
}
}
~Example()
{
Dispose(false);
}
}