473,387 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,387 software developers and data experts.

Memory question when returning an object from inside another object

Hi,

Quick overview of the problem:

public bool Something( out DataSet ds )
{
bool ret=false;

try
{
xmlDocument myXmlDoc = new xmlDocument( myFile.xml );

ds = myXmlDoc.DataSet;

ret=true;
}
catch( Exception e )
{
//Do something
}

return ret;
}

How would the memory for the xmlDocument get released? The object
calling this function would have a reference (correct me if I am wrong)
to the DataSet inside the xmlDocument, so would the c# memory manager
ignore this object?

I thought about replacing:

ds = myXmlDoc.DataSet;

with:

ds = myXmlDoc.DataSet.Copy();
myXmlDoc = null; //Would this allow memory to be released?

Any help on this would be appreciated.

Regards,

Steven



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
10 1651
I'm sure Jon Skeet will be watching and add his two pennies worth (which is
more than my opinions are valued at!) but I think it goes something like
this:

myXmlDoc is a variable that references an XmlDocument object. This
XmlDocument object is then referenced by a DataSet object. Therefore the
object is only "cleaned up" by the garbage collector when the object is no
longer being referenced either this XmlDocument variable or the DataSet
object... You don't need to call the Copy() method unless you want a
different object, copied from your original XmlDocument.

I do believe you can also do this:
public bool Something ( Dataset ds )
which passes in a reference to the dataset object. Therefore changes to this
object are reflected in the original.

"Steven Blair" <st**********@btinternet.com> wrote in message
news:OE**************@TK2MSFTNGP11.phx.gbl...
Hi,

Quick overview of the problem:

public bool Something( out DataSet ds )
{
bool ret=false;

try
{
xmlDocument myXmlDoc = new xmlDocument( myFile.xml );

ds = myXmlDoc.DataSet;

ret=true;
}
catch( Exception e )
{
//Do something
}

return ret;
}

How would the memory for the xmlDocument get released? The object
calling this function would have a reference (correct me if I am wrong)
to the DataSet inside the xmlDocument, so would the c# memory manager
ignore this object?

I thought about replacing:

ds = myXmlDoc.DataSet;

with:

ds = myXmlDoc.DataSet.Copy();
myXmlDoc = null; //Would this allow memory to be released?

Any help on this would be appreciated.

Regards,

Steven



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
I guess you mean XmlDataDocument, not XmlDocument?

I believe the XmlDataDocument will not be eligeble for GC if you
directly assign the XmlDataDocument.DataSet property to your out
parameter. The reason is that the XmlDataDocument registers event
handler methods with the DataSet in order to be able to update the
document as soon anything changes in the DataSet. This means that the
DataSet in fact has a reference (well several actually) to the
XmlDataDocument.

That aside, the code below will not work. The XmlDataDocument class does
not expose any constructor that takes a path to an XML-document, and the
XmlDocument class does not expose a DatSet property.

Is this really your repro case?

/Joakim

Steven Blair wrote:
Hi,

Quick overview of the problem:

public bool Something( out DataSet ds )
{
bool ret=false;

try
{
xmlDocument myXmlDoc = new xmlDocument( myFile.xml );

ds = myXmlDoc.DataSet;

ret=true;
}
catch( Exception e )
{
//Do something
}

return ret;
}

How would the memory for the xmlDocument get released? The object
calling this function would have a reference (correct me if I am wrong)
to the DataSet inside the xmlDocument, so would the c# memory manager
ignore this object?

I thought about replacing:

ds = myXmlDoc.DataSet;

with:

ds = myXmlDoc.DataSet.Copy();
myXmlDoc = null; //Would this allow memory to be released?

Any help on this would be appreciated.

Regards,

Steven



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3
Thanks for taking the time to reply.

The reason why there is a few typos is the source code isnt available to
me right now (its sitting on another PC which is locked atm)

Yes its a XmlDataDocument...

I can't remember the code directly, but I load a xml file into this
object. It might have a LoadXML mehtod.

So when I call ds = myXMLDoc.DataSet this prevents the XmlDataDocument
memory from being released... How would I ensure that the memory for
this object is released?

Another quick question:

ds = myXMLDoc.DataSet;

Would this create a new DataSet, or would ds be a reference to the
DataSet held inside the XMLDataDocument?

Regards,

Steven

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
Are you using the XmlDataDocument just to fill a DataSet with data?
Without needing the synchronization? Looking at your code example, I
think this would perform the same operation:

public bool Something( out DataSet ds )
{
bool ret=false;

try
{
ds = new DataSet();
ds.ReadXml( myFile.xml );

ret=true;
}
catch( Exception e )
{
//Do something
}

return ret;
}

Otherwise I guess you would have to use DataSet.Copy(), but that feels
like too much overhead.

Steven Blair wrote:
Another quick question:

ds = myXMLDoc.DataSet;

Would this create a new DataSet, or would ds be a reference to the
DataSet held inside the XMLDataDocument?


That will return a reference to XmlDataDocument's dataSet member.

/Joakim
Nov 16 '05 #5
Yeah looks like a little overkill I agree, but my workmate wrote the
code in the first place (seriously not passing the blame ;) ) and is
using a XSD file I think (this is not my area but I am assured it is
required). All the rows are then required to be loaded into a RDBMS, so
he uses the DataSet object from the xmlDataDocument.

He then uses a foreach (DataRow dr in ds.Tables[0],Rows) and calls a
Stored procedure to laod the transaction in.

Our first worry was the memory would never be released for the
xmlDataDocument, but I am sure the .Copy method will allow the memory to
be released.

Regards,

Steven

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #6
Hi,
AFAIK XmlDocument does not have a DataSet property. so the line

ds = myXmlDoc.DataSet;

will not compile

How would the memory for the xmlDocument get released? The object
calling this function would have a reference (correct me if I am wrong)
to the DataSet inside the xmlDocument, so would the c# memory manager
ignore this object?

The memory will be released after it's not longer used.

What happen depends of how the DataSet property is implemented ( at least in
theory, remember it does not exist ! )

XmlDocument keeps its data in an intermal data structure, it may be
something like a tree or a linked list. IF the dataset can use the same one
it would simple get a reference to it , if not it may transform it to its
intermal representation used. If DataSet kept a reference it will not
deleted once the XmlDocumet instance is out of scope, otherwise once the
XmlDocument is uit of scope the GC will recall the memory.
Hope you understand the above, it's a complex thing to explain and maybe my
english is not clear enough :)

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Nov 16 '05 #7
Hi,
I would try to use the DataSet directly this will save you an innecesary
step, if this is not possible then try to use the XmlDataDocument directly,
I see no neeed to use both if all what you want is store the data in a RDBMS
, you could iterate in the Data nodes of the XML document and create the
SqlCommands directly from there, no need to create a dataset.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Steven Blair" <st**********@btinternet.com> wrote in message
news:ed**************@TK2MSFTNGP10.phx.gbl...
Yeah looks like a little overkill I agree, but my workmate wrote the
code in the first place (seriously not passing the blame ;) ) and is
using a XSD file I think (this is not my area but I am assured it is
required). All the rows are then required to be loaded into a RDBMS, so
he uses the DataSet object from the xmlDataDocument.

He then uses a foreach (DataRow dr in ds.Tables[0],Rows) and calls a
Stored procedure to laod the transaction in.

Our first worry was the memory would never be released for the
xmlDataDocument, but I am sure the .Copy method will allow the memory to
be released.

Regards,

Steven

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #8
"Steven Blair" <st**********@btinternet.com> wrote in message
news:OE**************@TK2MSFTNGP11.phx.gbl...
How would the memory for the xmlDocument get released? The object
calling this function would have a reference (correct me if I am wrong)
to the DataSet inside the xmlDocument, so would the c# memory manager
ignore this object?


Speaking to the general case (ie, ignoring issues specific to
XmlDataDocument that others have addressed): There is no DataSet "inside"
the xmlDocument. There is a DataSet and there is an XmlDocument. The
XmlDocument holds a reference to the DataSet, and something else also holds
a reference to the DataSet. Once the function ends, nothing will hold a
reference to the XmlDocument, so it will be released -- releasing it's
reference to the DataSet in the process. The DataSet will remain, as there
remains a reference to it.

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Nov 16 '05 #9
James,

Thats makes things a bit clearer. I assumed the DataSet was part of the
xmlDataDocument.

One thing which still puzzles me is the releasing of the memory for the
xmlDataDocument.

IF I did this:

return xmlDataDocument.DataSet; //pass to calling func

Would the xmlDataDocument get released by the memory manager after the
fucntion has been called, even if the DataSet return was still getting
used?

This is what I am more confused on than anything.

Regards,

Steven

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #10
"Steven Blair" <st**********@btinternet.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
IF I did this:

return xmlDataDocument.DataSet; //pass to calling func

Would the xmlDataDocument get released by the memory manager after the
fucntion has been called, even if the DataSet return was still getting
used?


Certainly. Remember, "xmlDataDocument.DataSet" is just a variable
holding a reference to a DataSet object which lives elsewhere. It's
exactly as if you had written:

DataSet ds = xmlDataDocument.DataSet;
return ds;

Actually, it's more like:

DataSet ds = new DataSet();
// fill ds
xmlDataDocument.DataSet = ds;
return ds;

except you didn't show the first part.
--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Nov 16 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: florian kno | last post by:
hello all! i'm using gcc 3.2 on linux. i have a (self developed) matrix class: class cMatrix { // unneccessary info stripped .... double *data; // matrix data is stored in a heap array ...
4
by: Asfand Yar Qazi | last post by:
Sorry about this, but its driving me up the wall. First some code: typedef unsigned int size_t; struct AddOp { template<class I1, class I2> static inline float call(size_t i, const I1&...
4
by: Gurikar | last post by:
HI, class A() { private: int i; char c; public: A();
1
by: trialproduct2004 | last post by:
Hi all, I am having slight confusion regarding memory management in .net. Say suppose i have two application one is in C# and other is in MFC(VC++). Both of this application are using lots...
3
by: Richard | last post by:
I need to dynamically allocation memory at run time for the number of student's records and their test scores for the program code below. I don't understand what the 3 errors i got. I can anyone...
1
by: Mladen Adamovic | last post by:
Why is memory safe that function allocate inner object and return its result? That inner object is destroyed when function come to the "return" statement. Is default copy contructor doing some job?...
7
by: krishna81m | last post by:
In the following code snippet, I created a class samp which has two private variables /char/ and /char*/ which are initialized in the constructor. I create a function /samp input()/ that returns an...
0
by: Philip Semanchuk | last post by:
On Oct 22, 2008, at 8:33 PM, Robert Kern wrote: I agree. To deny users of this module access to this param of shmat() would be design arrogance on my part. To do so would be to claim that I...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.