[dba-VB] C# - Cleanup

jwcolby jwcolby at colbyconsulting.com
Wed Jul 28 21:29:20 CDT 2010


Yep.

John W. Colby
www.ColbyConsulting.com


Michael Maddison wrote:
> Looks like a good candidate for adding to your base data access class.
> 
> Cheers
> 
> Michael M
> 
> -----Original Message-----
> From: dba-vb-bounces at databaseadvisors.com
> [mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of jwcolby
> Sent: Thursday, 29 July 2010 9:42 AM
> To: VBA
> Subject: [dba-VB] C# - Cleanup
> 
> I have been reading up on the proper way to cleanup unmanaged code.
> Basically what this entails is inheriting CriticalFinalizerObject and
> IDisposable, and then adding stuff to the class.  The finalizer is used
> to dispose of unmanaged ubjects "deterministically", IOW when I say to,
> not when the garbage collector decides to.
> 
> This becomes an issue when you open a lot of connections.  The garbage
> collector does not understand that the connections exist and so performs
> it's cleanup when it gets around to it.
> 
> At least that is what I am understanding.  Doing a massive rewrite to
> add this finalizer code to all my classes that use such code has dropped
> my errors DRASTICALLY!
> 
> The code looks like this:
> 
>          #region Dispose
> 
>          ~clsSPAccuzipExportTo()
>          {
>              Dispose(true);
>          }
>          public void Close()
>          {
>              Dispose(true);
>              // This object will be cleaned up by the Dispose method.
>              // Therefore, you should call GC.SupressFinalize to
>              // take this object off the finalization queue
>              // and prevent finalization code for this object
>              // from executing a second time.
>              GC.SuppressFinalize(this);
>          }
>          // Implement IDisposable.
>          // Do not make this method virtual.
>          // A derived class should not be able to override this method.
>          public void Dispose()
>          {
>              Dispose(true);
>              // This object will be cleaned up by the Dispose method.
>              // Therefore, you should call GC.SupressFinalize to
>              // take this object off the finalization queue
>              // and prevent finalization code for this object
>              // from executing a second time.
>              GC.SuppressFinalize(this);
>          }
>          // Dispose(bool disposing) executes in two distinct scenarios.
>          // If disposing equals true, the method has been called
> directly
>          // or indirectly by a user's code. Managed and unmanaged
> resources
>          // can be disposed.
>          // If disposing equals false, the method has been called by the
>          // runtime from inside the finalizer and you should not
> reference
>          // other objects. Only unmanaged resources can be disposed.
>          private void Dispose(bool disposing)
>          {
>              // Check to see if Dispose has already been called.
>              if (!this.disposed)
>              {
>                  // If disposing equals true, dispose all managed
>                  // and unmanaged resources.
>                  if (disposing)
>                  {
>                      if (mCnn != null)
>                      {
>                          try
>                          {
>                              mCnn.Close();
>                          }
>                          catch (Exception)
>                          {
>                          }
>                          mCnn.Dispose();
>                      }
>                  }
> 
>              }
>              disposed = true;
>          }
>          #endregion
> 
> 
> --
> John W. Colby
> www.ColbyConsulting.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
> 
> 



More information about the dba-VB mailing list