index > Visual C# General > Implementing IDisposible, question mainly about releasing managed...

Implementing IDisposible, question mainly about releasing managed...

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);

 }

}

Jason-Massey

You can move the code in performDisposeUnmanaged to performDisposeManaged. You don't need the performDisposeUnmanaged method or the finalizer since you don't directly encapsulate any native resources.




Mattias, C# MVP
Mattias Sjögren

Thanks for your reply, but I don't understand why your saying to remove the performDisposeUnmanaged method, as the m_workflowDataAccess object is using database connections. I thought anything that used database connections (unmanaged finite) resource had to be placed in the performDisposeUnmanged.

As regard to the finalizer this was left in as a fail safe incase Dispose was not called by one of my implementing functions.

Is the above not correct?

I'm still unsure about releasing managed resources, and if I should be realising anything thats a reference type?

Jason-Massey
Jason-Massey wrote:

Thanks for your reply, but I don't understand why your saying to remove the performDisposeUnmanaged method, as the m_workflowDataAccess object is using database connections. I thought anything that used database connections (unmanaged finite) resource had to be placed in the performDisposeUnmanged.

What resources it uses internally is an implementation detail the user of the class shouldn't have to care about. All you should do is call the Dispose method if there is one when you're done with the object.

Jason-Massey wrote:

As regard to the finalizer this was left in as a fail safe incase Dispose was not called by one of my implementing functions.

Is the above not correct?

By the time your finalizer runs it's usually too late to call Dispose. If you're the only one holding a reference to the workflow object, it will probably be eligible for garbage collection and finalized at the same time.

Jason-Massey wrote:

I'm still unsure about releasing managed resources, and if I should be realising anything thats a reference type?

No you really only have to make sure you call Dispose on objects you own that provides such a method.




Mattias, C# MVP
Mattias Sjögren

But what happens about reference type objects that don?? have a Dispose method.  I?? under the belief that if I turn of the garbage collector as in the above code, then these objects will not be released from memory with the garbage collector disabled, and I should at least set these objects to null.

 

Any light you can shed on the above I would be gratefull.

 

Jason-Massey

Objects that don't have a Dispose method don't need any additional cleanup. You haven't turned off the garbage collector, that's not possible.




Mattias, C# MVP
Mattias Sjögren

Thanks for your help. Maybe i'm getting confused with what GC.SuppressFinalize actually does, I presumed this turned off the garbage collection for the whole object including any reference objects that had been declared. This meaning any reference objects must be set to null before the scope of the object is lost.

Coule you please clarify the above.

Jason-Massey
SuppressFinalize just prevents the Finalize method (~ClassName) from being called, it doesn't prevent the object from being garbage collected. In fact it causes the memory to be collected sooner than it otherwise would, that's why it's a good thing.


Mattias, C# MVP
Mattias Sjögren
reply 8

You can use google to search for other answers

 

More Articles

• Problems in converting colors
• How to get mem-size of a structure?
• Can I have multiple selection in my list box through XAML?
• How can I restore the main window after minimize the main window?...
• loading Image
• new browser in C#
• assign value to datetimepicker (windows form)?
• How to restart from Standby or Hibernation
• Communication between a windows form and a web application
• Tablet/Mobile PC development forum now open
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• createing something like MS word or note
• using Assembly language (not MSIL) in C#
• Thread Synchronization C# --> C++
• Chinese characters in a C# windows form
• Challenge here
• .NET feature that escapes certain charac
• does the finally block execute in this e
• Syncing data - locking a public property?
• Windows switching
• VS 2005: "Key not valid for use in
• how do i compare string formats?!
• Safe UI Wireup in Multithreaded Applicat
• How to handle Call Interrupt with in the
• Are there excellent steps for a new lear
• exit application after aborting thread

Hot Articles

• Limiting Bandwidth Per Application or Pe
• how to lost the focus of datagridview
• PublicKeyToken=null' could not be loaded
• Random give the same value
• How to detect windows service installed
• Namespace for FtpClientException
• Show richtexbox search result
• convert from gray to color..
• Threading - ActiveX Events/Objects
• StreamReader and File Position
• Force download
• Unable to see Type and Members dropdowns
• including quotations
• Reading regional settings
• non-modal MessageBox

Recommend Articles

• Gridview using BusinessLogic doesn't dis
• Accessing Network Drive with Credentials
• how can I deep clone an instance of any
• Minor Deployment Issue
• set desktop background with c#
• Windows Explorer details style
• Please write the linksof Free C sharp Ve
• haw can I load data from DataGridViewCom
• About returning @@Identity
• best practices to secure c# applications
• no My object in c# ?
• Low Performance
• Drawing shapes
• Form error message after upgrade form Be
• Capture Webcam