IDispose grrrrrr

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Ok, Im at my wits end with trying to dispose of this class. Everything I try
either doesnt dispose the class or I get build error. Im using C#. What Im I
doing wrong?

XHTML_Library objXHTMLHolder = new XHTML_Library();
objXHTMLHolder.Method...
..
..
objXHTMLHolder.Method...
..
..
objXHTMLHolder.DisposeMyClass();
Response.End();
======================
public class XHTML_Library : XmlDocument //want to dispose the whole class
{
..
..
..
public void DisposeMyClass()

{
//im sure issue is here. How to I properly call IDispose here??
this.Dispose();
}

}
 
Try declaring your class like this to expose the IDisposable interface

public class XHTML_Library : XmlDocument, IDisposable
 
If I place just before the Response.End() I get:
Exception of type System.StackOverflowException was thrown.

XHTML_Library objXHTMLHolder = new XHTML_Library();
..
..
..
objXHTMLHolder.Dispose();
Response.End();
================================
public class XHTML_Library : XmlDocument,IDisposable
{
public void Dispose()
{
Dispose();
GC.SuppressFinalize(this);
}
}
 
The overflow exception is because you are calling Dispose from within itself,
so it is obviously recursive.

The question is, why does your class need a dispose method? Does it contain
unmanaged members, or members that require disposing? If so, then this clean
up should be called within Dispose. Implementing a Dispose method and calling
it in itself does not do anything.

Might be worth reading up on the Dispose pattern to make sure you need it
and fully understand it.

HTH
Dan
 
Back
Top