472,353 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 1620
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: wh | last post by:
I have a class which when instatiated creates a new XmlDocument object and loads an xml file into it. It is used as follows: MyObject obj = new...
1
by: Mullin Yu | last post by:
I want to create a C# GUI application that will start a new console application, e.g. console.exe when clicking a button. Then, if click a button,...
2
by: Billy Porter | last post by:
Greetings, I got a class that wraps the System.Data.SqlClient.SqlConnection class (no COM interaction). I'm not sure if I'm supposed to implement...
16
by: Daniel Mori | last post by:
If an object implements the IDisposable interface (regardless if its a framework object or a user object), should I always dispose of that object...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the...
71
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to...
13
by: Carl Johansson | last post by:
Being quite new to C#, I may have misunderstood this. If so please bear with me! As far as I can understand, any instances of a class that...
4
by: cgarcia0117 | last post by:
For any class I write in C# that has a member variable that implements IDisposable my class implements the IDisposable pattern. I do this to...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.