473,544 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "Implementi ng a Dispose
Method "(http://msdn.microsoft.com/library/default.asp?
url=/library/en-
s/cpguide/html/cpconimplementi ngdisposemethod .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 = ConnectionStrin g
cn = New SqlConnection(S qlConnString)

End Sub


Public Overloads Sub Dispose() Implements
IDisposable.Dis pose
Dispose(True)
GC.SuppressFina lize(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 1700
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 "Implementi ng a Dispose
Method "(http://msdn.microsoft.com/library/default.asp?
url=/library/en-
s/cpguide/html/cpconimplementi ngdisposemethod .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 = ConnectionStrin g
cn = New SqlConnection(S qlConnString)

End Sub
Public Overloads Sub Dispose() Implements
IDisposable.Dis pose
Dispose(True)
GC.SuppressFina lize(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
331
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 MyObject("books.xml"); If I later have obj = new MyObject("books2.xml"); Would all memory referenced by 'obj' (i.e. the XmlDocument) from the first
1
5923
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, it will kill a specific console application. Am I right that I need to create a new thread when clicking a "Start Button" for starting a console application. And, a separating thread of "Stop...
2
4813
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 the IDisposable pattern for this wrapper or not. Since one of it's members (SqlConnection) implements this interface, I'm thinking maybe I ought to. But on the other hand, those unmanaged...
16
4235
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 out of principle?
156
5746
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 dispose of the following object that was created inside a method call. dim myvar as new object1 object1.dosomethingmethod(new object2) Note that object 2 has a dispose method but how do I...
5
19566
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 WSDL defines the output as an Object so I was having a problem in the calling program. I searched online and found suggestions that I return an Array instead so I modified my code (below) to return...
71
10690
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 make sure it is disposed the next time I check. Or is that automatic? Thanks
13
1876
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 implements the IDisposable interface must call the Dispose method not create leaks of resources!? This can be accomplished by explicitly calling Dispose or through the "using" statement. For example,...
4
9684
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 guarantee the reference to the member is explicitly released and the object is eligible for garbage collection when my class is disposed or its' finalizer is called by GC. My question is whether using...
0
7452
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7387
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7798
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7405
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7738
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5956
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1862
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.