5 Replies - 357 Views - Last Post: 02 December 2011 - 07:51 AM Rate Topic: -----

#1 scolty  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 259
  • Joined: 27-April 11

Dispose() and finalizer question

Posted 02 December 2011 - 04:09 AM

Morning,

Im currently a little confused regarding the implementation of the Dispose() method in the context of unmanaged resources, in particular database connections. I understand that GC cleans up all object that dont have unmanaged resources. I understand that i can use the using statement to enclose my database connection so that dispose() is automatically closed. what i dont understand is the following:

1) If i need a connection to remain open for longer (ie cant use a using statement) then what would the code be to explicitly close the connection?

2) If i have a dispose() method then i should also include a finalizer in the class declaration. I should also follow the pattern such that if the dispose()method is called before the finalizer then i should call GC.Suppressfinalize(this) method since i would have, assuming i havent screwed up, already disposed of the unmanaged resources. Have i understood this correctly?

msdn link

3) Are their any limitations/things to keep in mind when a class (aka ClassA) uses a method in another class (aka classB) and classB uses unmanaged resources? Do i need to call a Dispose() method on classA??

1) is the main question so id appreciate it if someone would atleast be willing to answer that.

Thanks

This post has been edited by scolty: 02 December 2011 - 04:10 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Dispose() and finalizer question

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Dispose() and finalizer question

Posted 02 December 2011 - 05:16 AM

are you speaking about SQL(Oledb) connection?
Was This Post Helpful? 0
  • +
  • -

#3 scolty  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 259
  • Joined: 27-April 11

Re: Dispose() and finalizer question

Posted 02 December 2011 - 05:53 AM

Yes thats correct, ie connection would be

System.Data.SqlClient.SqlConnection conn = 
        new System.Data.SqlClient.SqlConnection (); 


what i dont get is how to dispose of it. Im assuming i need to declare a dispose method in the class which has the above connection code in it. Inherrit the IDisposable interface onto that class as well. Before.... just writting
conn.dispose()
???

if so, what do i have to/what should i write in the dispose method declaration? All the books i have just have single line comments saying dispose of unmanaged resources here... which, as u can imagine, doesnt really help me lol.

Thanks again
Was This Post Helpful? 0
  • +
  • -

#4 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Dispose() and finalizer question

Posted 02 December 2011 - 06:05 AM

well yea but first you have to close the connection and then dispose. though come to think of it even if you just close the connection it will dispose it it self(i think i`m not 100% sure). you can make your own class a dispose method or finish or what ever you want to call it when you must check if the connection is still opened to close it and the dispose it and any other things that you want to dispose. though in .NET you have automatic GC which will dispose of them (except the connection if its not closed). In short the most important thing about connection is to always close it after you are done with it. In Access it can cause your database to crash and be corrupted.

This post has been edited by NoBrain: 02 December 2011 - 06:07 AM

Was This Post Helpful? 0
  • +
  • -

#5 scolty  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 259
  • Joined: 27-April 11

Re: Dispose() and finalizer question

Posted 02 December 2011 - 07:20 AM

Ok, im still a little confused. MSDN has the following code:


// Design pattern for a base class.
public class Base: IDisposable
{
   //Implement IDisposable.
   public void Dispose() 
   {
     Dispose(true);
      GC.SuppressFinalize(this); 
   }

   protected virtual void Dispose(bool disposing) 
   {
      if (disposing) 
      {
         // Free other state (managed objects).
      }
      // Free your own state (unmanaged objects).
      // Set large fields to null.
   }

   // Use C# destructor syntax for finalization code.
   ~Base()
   {
      // Simply call Dispose(false).
      Dispose (false);
   }
   
// Design pattern for a derived class.
public class Derived: Base
{   
   protected override void Dispose(bool disposing) 
   {
      if (disposing) 
      {
         // Release managed resources.
      }
      // Release unmanaged resources.
      // Set large fields to null.
      // Call Dispose on your base class.
      base.Dispose(disposing);
   }
   // The derived class does not have a Finalize method
   // or a Dispose method with parameters because it inherits
   // them from the base class.
}




if i had my connection statement in the derived class, would i just call
 this.dispose(true); 
once i had finished with the connection and then with the line:

 protected override void Dispose(bool disposing) 
   {
      if (disposing) 
      {
         // Release managed resources.
      }
      // Release unmanaged resources.



just write

 conn.dispose(); 
?? im pretty sure thats wrong but this is what im really confused about.
Was This Post Helpful? 0
  • +
  • -

#6 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Dispose() and finalizer question

Posted 02 December 2011 - 07:51 AM

well yes but remember to close the connection too. btw in msdn they say that you can simply just close the connection and dont use dispose. but i cant find the link right now.
EDIT: i think you should take a look at this article because i think you're confused what is managed and unmanaged code
http://www.c-sharpco...ed-code-in-net/

This post has been edited by raziel_: 02 December 2011 - 07:58 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1