|
I wonder if someone with more knowlage then me about the following can check, and possibly clear up that i'm coding the following correct.
The following code is a code fragment, and it shows how I implement my own Dispose method, as you can see from the class it's inheriting the IDisposable interface. I have two questions.
A) First could someone check over this code and make sure everything appears correct, I check over I'm refering to the Dispose methods, contructor and deconstructer.
B) Second i'm a little unclear what resources should be released inside 'performDisposeManaged'. I'm under the impression it's any resource thats managed, and is a reference type. Value types, such as strings and integer etc do not need to be cleared up.
public class Template : IDisposable
{ // Added for responses protected
static int m_listboxId; protected string m_rawXML;
string m_appPath;
protected string m_viewTemplate;
string m_htmlTemplate;
protected XMLTemplate m_xmlTemplate;
XMLFunctions m_xmlFunctions;
int m_templateId;
protected WorkflowDataAccess m_workflowDataAccess;
protected ParameterControl m_parameterControl;
object m_syncBlock;
bool m_isDisposed = false;
public Template()
{
m_workflowDataAccess = new WorkflowDataAccess();
m_parameterControl = new ParameterControl();
m_xmlFunctions = new XMLFunctions();
m_syncBlock = new object();
}
protected virtual void Dispose(bool disposing)
{
// Use a syncBlock to protect against multiple disposing of objects in multi threaded enviroment. lock (m_syncBlock)
{
if (!m_isDisposed)
{
if (disposing == true)
{
performDisposeUnmanaged();
}
else
{
performDisposeManaged();
}
m_isDisposed = true;
GC.SuppressFinalize(this);
}
else
{
//throw new ObjectDisposedException(RetError("Template", "Dispose", "Object already disposed"));
RetError("Template", "Dispose", "Object already disposed");
}
}
}
~Template()
{
Dispose(false);
}
protected virtual void performDisposeUnmanaged()
{ // Release any unmanaged resources
if (m_workflowDataAccess != null)
{ m_workflowDataAccess.Dispose();
m_workflowDataAccess = null;
}
}
private void performDisposeManaged()
{ // Release any managed resources
if (m_xmlFunctions != null)
{ m_xmlFunctions = null;
}
if (m_parameterControl !=null)
{ m_parameterControl = null;
}
}
public void Dispose()
{
Dispose(true);
}
} |