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
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:
public class MyClass:MyBaseClass
{
public MyClass(string ConnString):base(string ConnString)
{
}
~MyClass()
{
}
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.