implement dispose method

B

Billy

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

*********************************************
 
S

Scott M.

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.


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

*********************************************
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top