Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can someone please clear up something for me? I don't quite understand how to properly handle an un-managed resource from a base class. For example, say I have a base class that I want to do basic connectivity to a database. Something like
C#
public abstract class MyBaseClass
{
  SQLiteConnection MyConnection {get;set;}
  public MyBaseClass(string connString)
{
 this.MyConnection = new SQLiteConnection(connString);
this.MyConnection.Open();
}
  ~MyBaseClass()
{
  if (this.DBConn != null)
{
    this.DBConn.Close();
    this.DBConn.Dispose();
}
}


And a the inheriting class:

C#
public class MyClass:MyBaseClass
{
 public MyClass(string ConnString):base(string ConnString)
{ 
 
}

~MyClass()
{
//what do I need to do here?
}


My understanding is that when an instance of MyClass goes out of scope, GC will at some point dispose of it. When it does it will call the ~MyClass() destructor. (Correct?) So, when MyClass is getting cleaned up, will it automatically call the base destructor? This is a little hard to test since the GC.Collect is indeterminate, and in my case it is on a thread whose lifetime is indeterminate.

I am planning on calling these types from a thread where its lifetime is not predictable, and there will likely be multiple instances at the same time. In this circumstance I had previously implemented with the IDisposable interface, but the above seems much easier if I'm doing it right. I guess I'm just not clear what GC is going to do with the ~destructors both in the partent and base class.

Thanks as always.
Posted

The preferred approach in .NET is to use Dispose pattern for clean up of unmanaged resources, as it is not certain at what time Finalize method is called by the GC. Please go through the following articles

Implementing IDisposable and the Dispose Pattern Properly[^]
http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx[^]
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 18-Mar-12 22:34pm    
That is correct, my 5; I only would add that it's often not just preferred. If the class using such resources is already defined in the library, there is no other choice. Also, IDisposable can be used in situations not related to the resources...
--SA
ProEnggSoft 18-Mar-12 22:51pm    
Thank you for more info and upvote.
The perfered method for the disposal of resources is the Dispose method, not the destructor. When finished using a resource, and this is only required if a class needs to dispose of some resource, then call the Dispose method when finished using the class that is using the resource. That means that if there is a resource that a class uses, then should include a Dispose method. The reason for this is because explicitly using a Displose method (and calling it when finished with a class) means the Memory Management does not have to deal with, releasing the resources earlier. Also implementing a dispose method means that the Using statement can be used, which will automatically Displose of the resource once the code within the scope of the using statement exits this scope. Generally with .Net code within the ~method will actually call the Dispose method to cover the base that the programmer did not call the dispose method.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900