implement dispose method

Discussion in 'ASP .Net' started by Billy, Nov 12, 2003.

  1. Billy

    Billy Guest

    Hello...

    I'm trying to make a database access class for an asp.net
    application. When I run my application, the Garbage
    Collecter doesn't seems to unload the memory attributed
    to my SQLConnection. My db access class has a method
    that returns a dataset and one that executes a non-reader
    request. It also has a dispose method (to release the
    connection) that I call in the finalize method of my aspx
    pages(as learn in "Implementing a Dispose
    Method "(http://msdn.microsoft.com/library/default.asp?
    url=/library/en-
    s/cpguide/html/cpconimplementingdisposemethod.asp). All
    of the classes in the application inherits from the db
    access class to facilitate the communication with the
    db. I wan't to know if I have to call the dispose method
    of the classes that inherits the db access class each
    time I instanciate one.

    Here's some code

    Public Class Connexion
    Implements IDisposable

    Public SqlConnString As String
    Public cn As SqlConnection
    ' Track whether Dispose has been called
    Private disposed As Boolean = False


    Sub New()

    SqlConnString = ConnectionString
    cn = New SqlConnection(SqlConnString)

    End Sub


    Public Overloads Sub Dispose() Implements
    IDisposable.Dispose
    Dispose(True)
    GC.SuppressFinalize(Me)
    End Sub

    Protected Overridable Overloads Sub Dispose(ByVal
    disposing As Boolean)
    ' If the method is not already called
    If Not (Me.disposed) Then
    ' Si c'est appellé par le code
    If (disposing) Then
    ' Dispose managed resources.
    cn.Dispose()
    End If
    End If
    Me.disposed = True
    End Sub
    Protected Overrides Sub Finalize()

    Dispose(False)

    End Sub
    End Class
    **************************************************
    And a class that inherits...
    Public Class Class2

    Inherits Connexion

    [...]

    ' Track whether Dispose has been called.
    Private disposed As Boolean = False

    [...]

    Protected Overloads Overrides Sub Dispose(ByVal disposing
    As Boolean)
    If Not (Me.disposed) Then
    MyBase.Dispose(disposing)
    End If
    End Sub

    *********************************************
     
    Billy, Nov 12, 2003
    #1
    1. Advertising

  2. Billy

    Scott M. Guest

    An object's Dispose method will fire when the object falls out of scope.
    You can make an object Dispose earlier than that by setting the object to
    Nothing.


    "Billy" <> wrote in message
    news:002101c3a949$9512f460$...
    Hello...

    I'm trying to make a database access class for an asp.net
    application. When I run my application, the Garbage
    Collecter doesn't seems to unload the memory attributed
    to my SQLConnection. My db access class has a method
    that returns a dataset and one that executes a non-reader
    request. It also has a dispose method (to release the
    connection) that I call in the finalize method of my aspx
    pages(as learn in "Implementing a Dispose
    Method "(http://msdn.microsoft.com/library/default.asp?
    url=/library/en-
    s/cpguide/html/cpconimplementingdisposemethod.asp). All
    of the classes in the application inherits from the db
    access class to facilitate the communication with the
    db. I wan't to know if I have to call the dispose method
    of the classes that inherits the db access class each
    time I instanciate one.

    Here's some code

    Public Class Connexion
    Implements IDisposable

    Public SqlConnString As String
    Public cn As SqlConnection
    ' Track whether Dispose has been called
    Private disposed As Boolean = False


    Sub New()

    SqlConnString = ConnectionString
    cn = New SqlConnection(SqlConnString)

    End Sub


    Public Overloads Sub Dispose() Implements
    IDisposable.Dispose
    Dispose(True)
    GC.SuppressFinalize(Me)
    End Sub

    Protected Overridable Overloads Sub Dispose(ByVal
    disposing As Boolean)
    ' If the method is not already called
    If Not (Me.disposed) Then
    ' Si c'est appellé par le code
    If (disposing) Then
    ' Dispose managed resources.
    cn.Dispose()
    End If
    End If
    Me.disposed = True
    End Sub
    Protected Overrides Sub Finalize()

    Dispose(False)

    End Sub
    End Class
    **************************************************
    And a class that inherits...
    Public Class Class2

    Inherits Connexion

    [...]

    ' Track whether Dispose has been called.
    Private disposed As Boolean = False

    [...]

    Protected Overloads Overrides Sub Dispose(ByVal disposing
    As Boolean)
    If Not (Me.disposed) Then
    MyBase.Dispose(disposing)
    End If
    End Sub

    *********************************************
     
    Scott M., Nov 13, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SS
    Replies:
    2
    Views:
    454
    Trond-Eirik Kolloen
    Jul 3, 2003
  2. Leon
    Replies:
    12
    Views:
    11,427
    luxmit85
    Feb 12, 2008
  3. pete
    Replies:
    0
    Views:
    386
  4. Antonio Concepcion
    Replies:
    3
    Views:
    2,734
    Antonio Concepcion
    Feb 17, 2005
  5. =?Utf-8?B?U2ltb25lIEJ1c29saQ==?=

    When is IHttpModule.Dispose method called?

    =?Utf-8?B?U2ltb25lIEJ1c29saQ==?=, Jan 3, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    3,699
    =?Utf-8?B?U2ltb25lIEJ1c29saQ==?=
    Jan 4, 2006
Loading...

Share This Page