Question on implementing IDisposable

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



hi,

My question is:

1. To avoid possible memory leaks, when you use this pattern, after you
have dealth with the unmanaged resources and before you take your
object off the finalize queue, how are you sure that your managed
object resources are completely freed up of resources it's might be
using? In my case below I have a private bool variable. Are there any
other managed resource that you might need to explicitly free up in
Dispose?

2. I have the example in msdn that instanciates a Component object but
doesn't actually do anything with it, what is this class for?

Thanks
-phl

Here's some pseudo code:


public classA : IDisposable
{
private Component component = new Component(); //WHAT IS THIS
FOR????
private XmlTextReader reader;
private bool disposed = false;


public LoadFile(string FilePath)
{
reader = new XmlTextReader(FilePath);
}


public void CloseFile()
{
reader.Close();
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}


public void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
CloseFile();
//are there anything else you need to free up
here?????
}
}

this.disposed = true;
}

ClassA()
{
Dispose(false);
}
}

.



Relevant Pages

  • Re: VS2005 and SQL Express: Internal .Net Framework Data Provider
    ... connection object you use. ... public void CreateConnection{ ... // Close first and dispose the existing data reader is there ... release resources used by the object ...
    (microsoft.public.dotnet.framework.adonet)
  • VS2005 and SQL Express: Internal .Net Framework Data Provider erro
    ... public void CreateConnection{ ... // Check first if the connection is open, if yes, close it first ... // Close first and dispose the existing data reader is there is ... release resources used by the object ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: VS2005 and SQL Express: Internal .Net Framework Data Provider erro
    ... THe interal connection is underneath the ... public void CreateConnection{ ... // Close first and dispose the existing data reader is there is ... release resources used by the object ...
    (microsoft.public.dotnet.framework.adonet)
  • RE: what are unmanaged resources
    ... the GC will come along and clean up all the memory and resources associated ... in a deterministic fashion and clean up unmanaged resources. ... placed inside the Dispose functions. ... sooner than later (i.e. by waiting for the garbage collector) and you should ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: MODI - Cannot delete file after document closed
    ... I missed your suggestion about adding Marshal.ReleaseComObjectto my ... when I tried to delete the file at the end of the Dispose() call. ... public void TestMultiReads() ... OCRUtil ocr = new OCRUtil; ...
    (microsoft.public.dotnet.framework.interop)