Home > Misc > Stack overflow error while disposing object – C#

Stack overflow error while disposing object – C#

I have a class “ABC” which implements “IDisposable” interface and having below Dispose method.

Public Class ABC : IDisposable {

public void Dispose()       {




When I run the application I was getting “Stack Overflow” error because the Dispose() method getting called recursively and went in to an infinite loop.

Fix :

  • Prevent the Dispose() recursive call by using flag and the logic is as below

 private bool disposed = false;

public void Dispose() {




protected virtual void Dispose(bool disposing) {

if (this.disposed) {



this.disposed = true;



Categories: Misc Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: