jwcolby
jwcolby at colbyconsulting.com
Wed Dec 1 05:20:24 CST 2010
Vlad, Just as a class can have a constructor, it can have a destructor. The destructor (in c#) begins with the special character ~ Building a destructor is an art form ... http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx but basically looks like: class Car { ~Car() // destructor { // cleanup statements... } } All by itself building the destructor does not do you much good. Yes, it gives the GC something to call but the GC does that anyway. So we create a dispose method and a flag that says we have done that. http://www.informit.com/articles/article.aspx?p=101373&seqNum=13 public class Worker: System.IDisposable { private bool alreadyDisposed = false; public Worker() { System.Console.WriteLine("In the constructor."); } public void Dispose(bool explicitCall) { if(!this.alreadyDisposed) { if(explicitCall) { System.Console.WriteLine("Not in the destructor, " + "so cleaning up other objects."); // Not in the destructor, so we can reference other objects. //OtherObject1.Dispose(); //OtherObject2.Dispose(); } // Perform standard cleanup here... System.Console.WriteLine("Cleaning up."); } alreadyDisposed = true; } public void Dispose() { Dispose(true); System.GC.SuppressFinalize(this); } ~Worker() { System.Console.WriteLine("In the destructor now."); Dispose(false); } } Having done all this, you place your explicit calls to the dispose method of things like connection objects in your dispose method. Then you manually call your dispose method when you are done with the class. This just cleans up all of the unmanaged objects in an organized fashion. John W. Colby www.ColbyConsulting.com On 11/30/2010 10:53 PM, ACTEBS wrote: > Hi John, > > Would you please give me an example of how you use the destructor class to > close an open db connection? I've researched it, but can't conceptualise how > you'd set it up. > > Many thanks... > > Vlad > > -----Original Message----- > From: dba-vb-bounces at databaseadvisors.com > [mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of jwcolby > Sent: Wednesday, 1 December 2010 12:47 AM > To: Discussion concerning Visual Basic and related programming issues. > Subject: Re: [dba-VB] Close DB Connection > > I had a similar issue with connections to SQL Server. When I was > investigating the problem I > discovered that (in my case) the issue was that the garbage collector (GC) > was taking its sweet time > collecting the trash, and the connections were not released until the trash > was taken out. > > Understand that in my case I was creating classes which created a > collection, then I would destroy > the class. Since the GC does its thing whenever it decides that it needs to > free up memory, and > since I had a lot of memory, the GC decided to take its sweet time. > > Once I discovered destructors and dispose, my problems went away. > > John W. Colby > www.ColbyConsulting.com > > On 11/30/2010 8:06 AM, ACTEBS wrote: >> Hi Everyone, >> >> >> >> I've got this issue with my app where the app keeps it's connection to the >> Access DB open even after the application has finished it's task. I can't >> figure out for the life of me where I've missed closing it in the various >> Functions and Sub Routines. >> >> >> >> I've ensured that cn.Close and cn = Nothing, has been included in all the >> code where the DB is opened. The only place I can't include cn = Nothing > is >> when the connection is encapsulated within a Using Statement where for > some >> reason this is not allowed. Could this be the problem? If so, how can I >> release the connection? >> >> >> >> Also, is there any way of finding out which bit of code is keeping the >> connection open? >> >> >> >> Many Thanks >> >> >> >> Vlad >> >> >> >> >> >> >> >> _______________________________________________ >> dba-VB mailing list >> dba-VB at databaseadvisors.com >> http://databaseadvisors.com/mailman/listinfo/dba-vb >> http://www.databaseadvisors.com >> >> > _______________________________________________ > dba-VB mailing list > dba-VB at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-vb > http://www.databaseadvisors.com > > > > > > > > > _______________________________________________ > dba-VB mailing list > dba-VB at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/dba-vb > http://www.databaseadvisors.com > >