469,600 Members | 2,497 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,600 developers. It's quick & easy.

implement dispose method

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

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

Nov 17 '05 #1
1 1552
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" <bi***********@hotmail.com> wrote in message
news:00****************************@phx.gbl...
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

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


Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Mullin Yu | last post: by
2 posts views Thread by Billy Porter | last post: by
156 posts views Thread by Dennis | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.