473,499 Members | 1,614 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("books2.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 3889
wh <wa***@nospam.pyesmeadow.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("books2.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("books2.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.com>
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.pyesmeadow.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.com>
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 =...
4
1801
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...
16
2187
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...
1
1689
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...
8
8423
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
5729
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...
5
19558
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...
71
10667
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...
13
1870
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...
4
9677
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...
0
7134
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,...
0
7180
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,...
1
6905
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...
0
5485
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,...
1
4921
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...
0
4609
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...
0
1429
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 ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
311
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.