473,321 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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.
Jul 21 '05 #1
5 3589
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
Jul 21 '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.
Jul 21 '05 #3
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.
Jul 21 '05 #4
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
Jul 21 '05 #5
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
Jul 21 '05 #6

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 MyObject("books.xml"); If I later have obj =...
4
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
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
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
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
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
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
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
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
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.