473,624 Members | 2,026 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 1712
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
5930
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 Button" is used to stop a specific console application?? I tried to do the following, button the...
2
4819
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 resources has already been wrapped in the SqlConnection class... If so, how would my Dispose method look...
16
4247
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
5823
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 dispose of it unless I do the following:
5
19579
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 an Array instead of an ArrayList. Now I get the message when I try to run just my webservice...
71
10718
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
1887
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, a recursive method that creates hundreds or thousands of instances of, for example,...
4
9695
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 this approach is even necessary? My feeling is yes because it ensures the member is explicitly...
0
8238
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8174
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8624
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8336
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8478
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4082
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2607
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

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.