473,563 Members | 2,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Do I need to implement a Dispose method?

wh
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("books 2.xml");

Would all memory referenced by 'obj' (i.e. the XmlDocument) from the first
call be freed or would I be required to implement a Dispose method to
release the memory associated like:

public void Dispose()
{
m_Xml = null;
}

From what I can make out, as XmlDocument is a managed resource (with no
explicit Dispose() or Close() method) it will automatically be deallocated.

Wayne.
Nov 15 '05 #1
3 3896
wh <wa***@nospam.p yesmeadow.com> wrote:
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("books 2.xml");

Would all memory referenced by 'obj' (i.e. the XmlDocument) from the first
call be freed or would I be required to implement a Dispose method to
release the memory associated like:

public void Dispose()
{
m_Xml = null;
}

From what I can make out, as XmlDocument is a managed resource (with no
explicit Dispose() or Close() method) it will automatically be
deallocated.


Setting

obj = new MyObject("books 2.xml"); doesn't free up memory immediately,
but then neither does m_Xml = null;

You just don't free up memory immediately in .NET - the garbage
collector takes care of it. If you've got *unmanaged* resources within
your object, or references to any objects which themselves implement
IDisposable, you should dispose of those appropriately. However, that
should be a fairly rare occurrence, IMO.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
wh
Thank you for your reply.

So can I assume that *if* XmLDocument did implement a Dispose() method then
I should provide a Dispose() method in my class and call the Dispose() of
the XmlDocument?

Wayne.
Nov 15 '05 #3
wh <wa***@nospam.p yesmeadow.com> wrote:
So can I assume that *if* XmLDocument did implement a Dispose() method then
I should provide a Dispose() method in my class and call the Dispose() of
the XmlDocument?


Yes.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

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
4
1811
by: Johannes Hansen | last post by:
What are the best practice on handling an exception caused by a Dispose method when its called from inside a loop? Wrap the entire loop in a try-catch or do the try-catch on each iteration to get as many of the object Disposed as possible? Take the following example: Bitmap bmpArray = ... // Some fancy bitmap array. try {
16
2193
by: Joel Finkel | last post by:
Folks, I am confused as to how to implement the following solution. I have a series of processing steps, each of which contains similar features (forms, etc). Therefore, I create a base class, Step, and subclass from that for specific steps. The Step class has a method, Execute(), which can return either Success or Failure. I have a...
1
1703
by: Billy | last post by:
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...
8
8428
by: Howard Kaikow | last post by:
I've been trying to convert the following C# Dispose to VB .NET: protected override void Dispose(bool disposing) { if (excelThread != null) {
156
5779
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
19570
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
10694
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
1880
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
9687
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
7583
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
7885
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. ...
0
8106
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
7638
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...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
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
2082
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
1198
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.