473,396 Members | 1,929 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,396 software developers and data experts.

call functions that return MemoryStream or XmlDocument


Suppose I have the following functions:

Public Function myF1ToStream(...) As System.IO.MemoryStream
. . .
Dim ms As New System.IO.MemoryStream(buf)
Return ms
End Function

Public Function mcF2ToXml(. . .) As Xml.XmlDocument
. . .
Dim xmlDoc As New Xml.XmlDocument
xmlDoc.Load(ms)
Return xmlDoc
End Function
When I call these functions, which of the following lines marked with
('?) is necessary?
(I use vs2003, without 'Using' keyword. )

Dim ms As System.IO.MemoryStream = myF1ToStream(...)
'? ms.Seek(0, System.IO.SeekOrigin.Begin)
. . . ms.ReadByte() . . .
'? ms.Close()
'? ms = Nothing

Dim xmlDoc As XmlDocument = mcF2ToXml(...)
. . . myUse(xmlDoc) . . .
'? xmlDoc = Nothing

Thanks,

Atara
*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #1
10 2119
Atara,

I never used the seek in a memorystream, I always start at zero, so I don't
know if it in your case is necessary.

Setting something to nothing means only that it removes the reference, so
"you" can not direct use it anymore.

However that does not mean that it does not exist anymore, therefore it is a
little bit without sense.

Because it is asked so often, I have made this little piece of sample to
show it.

Drag for this a DataGridView and a Button on a form paste in this code and
run it.
\\\
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
Dim dc As New DataColumn("a", GetType(System.String))
dt.Columns.Add(dc)
dc.Dispose()
dc = Nothing
For i = 1 To 5
Dim dr As DataRow = dt.NewRow
dr.Item(0) = i.ToString
dt.Rows.Add(dr)
Next
DataGridView1.DataSource = dt
dt.Dispose()
dt = Nothing

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim dt As DataTable = DirectCast(DataGridView1.DataSource, DataTable)
For i = 6 To 10
Dim dr As DataRow = dt.NewRow
dr.Item(0) = i.ToString
dt.Rows.Add(dr)
Next
End Sub
///

Cor


"Atara" <At***@DD.comschreef in bericht
news:OF**************@TK2MSFTNGP03.phx.gbl...
>
Suppose I have the following functions:

Public Function myF1ToStream(...) As System.IO.MemoryStream
. . .
Dim ms As New System.IO.MemoryStream(buf)
Return ms
End Function

Public Function mcF2ToXml(. . .) As Xml.XmlDocument
. . .
Dim xmlDoc As New Xml.XmlDocument
xmlDoc.Load(ms)
Return xmlDoc
End Function
When I call these functions, which of the following lines marked with
('?) is necessary?
(I use vs2003, without 'Using' keyword. )

Dim ms As System.IO.MemoryStream = myF1ToStream(...)
'? ms.Seek(0, System.IO.SeekOrigin.Begin)
. . . ms.ReadByte() . . .
'? ms.Close()
'? ms = Nothing

Dim xmlDoc As XmlDocument = mcF2ToXml(...)
. . . myUse(xmlDoc) . . .
'? xmlDoc = Nothing

Thanks,

Atara
*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #2


I did not understand your code sample - do I need to dispose the
returned memoryStream, and the returned xmlDocument when I am done with
them?

I never so a sample code of returning such types from a function. In
sample code, this types are always used within one function (so I guess
that when the function ends its memory is collected)

thanks,

Atara

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #3
Atara,

Did you run the sample, I have put direct the dispose in it, because that
gives mostly the same misunderstandings as the nothing.

Try it and it should becoume clear.

You don't have to set in these situations something to nothing or to use the
dispose method. Simple because it takes only processing time and has not any
effect.

Cor
"Atara" <At***@DD.comschreef in bericht
news:%2****************@TK2MSFTNGP05.phx.gbl...
>

I did not understand your code sample - do I need to dispose the
returned memoryStream, and the returned xmlDocument when I am done with
them?

I never so a sample code of returning such types from a function. In
sample code, this types are always used within one function (so I guess
that when the function ends its memory is collected)

thanks,

Atara

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #4
On Tue, 24 Jun 2008 05:49:53 -0700, Atara <At***@DD.comwrote:
>
Suppose I have the following functions:

Public Function myF1ToStream(...) As System.IO.MemoryStream
. . .
Dim ms As New System.IO.MemoryStream(buf)
Return ms
End Function

Public Function mcF2ToXml(. . .) As Xml.XmlDocument
. . .
Dim xmlDoc As New Xml.XmlDocument
xmlDoc.Load(ms)
Return xmlDoc
End Function
When I call these functions, which of the following lines marked with
('?) is necessary?
(I use vs2003, without 'Using' keyword. )

Dim ms As System.IO.MemoryStream = myF1ToStream(...)
'? ms.Seek(0, System.IO.SeekOrigin.Begin)
. . . ms.ReadByte() . . .
'? ms.Close()
'? ms = Nothing

Dim xmlDoc As XmlDocument = mcF2ToXml(...)
. . . myUse(xmlDoc) . . .
'? xmlDoc = Nothing

Thanks,

Atara
I don't see how this one would be needed:
'? ms.Seek(0, System.IO.SeekOrigin.Begin)
You need to either call Close or Dispose to release resources.
'? ms.Close()
Otherwise the resources won't be released until the object is garbage
collected, which might not be for a long time.

Setting a reference to Nothing is rarely needed if the variable has a
local scope. If 'ms' will go out of scope very soon, then there is no
reason to set it to Nothing.
'? ms = Nothing
'? xmlDoc = Nothing
Same as above. If 'xmlDoc' will soon go out of scope, there is no
need. If 'xmlDoc' has a long life (e.g. Form property) then you
should set it to Nothing.

Removing all references to an object, either because the referencing
variable goes out of scope (gets destroyed) or because it is set to
Nothing, does not cause anything to happen immediately, it just makes
the object available for garbage collection.
Jun 27 '08 #5
Jack,

snip,
>
Removing all references to an object, either because the referencing
variable goes out of scope (gets destroyed) or because it is set to
Nothing, does not cause anything to happen immediately, it just makes
the object available for garbage collection.
snip

I tried to show with my sample that the latter part of this is only true, if
the object is not referenced anymore by any other object.

Cor

Jun 27 '08 #6

Please correct me if I mis-understood you:

1. Regarding the memoryStream, if f1() returns memoryStream, and f2()
uses it, (and it can happen 1000 times during my program,) I better
close the memoryStream, so it will be garbage-collected soon, but I do
not have to set it to Nothing???

Public Sub f2()
Dim ms As System.IO.MemoryStream = f1(...)
. . . ms.ReadByte() . . .
ms.Close()
End Sub

2. Regarding the xmlDocument - how do I force it to be garbage-collected
as soon as it is not used?
(at the beginning of my program, I read ~40 xml files, their total file
size is ~8M, but when creating from each xml file xmlDocument object, I
guess their size is larger)

Thanks,

Atara.

Thanks,

Atara.

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #7
>
1. Regarding the memoryStream, if f1() returns memoryStream, and f2()
uses it, (and it can happen 1000 times during my program,) I better
close the memoryStream, so it will be garbage-collected soon, but I do
not have to set it to Nothing???
Correct
Public Sub f2()
Dim ms As System.IO.MemoryStream = f1(...)
. . . ms.ReadByte() . . .
ms.Close()
End Sub

2. Regarding the xmlDocument - how do I force it to be garbage-collected
as soon as it is not used?
(at the beginning of my program, I read ~40 xml files, their total file
size is ~8M, but when creating from each xml file xmlDocument object, I
guess their size is larger)
Set them simple in a method, as soon as that goes out of scope and there is
no reference anymore to it from somewhere else, it simply is in the normal
procedure to be garbaged as soon as this is needed.

Forcing the GC has never been a success and as long as there is any
reference to an object, it is only doing a costly job in not wanted time.

Cor
Thanks,

Atara.

Thanks,

Atara.

*** Sent via Developersdex http://www.developersdex.com ***

Jun 27 '08 #8
Before I be misunderstood, each read in a method where the instancing is
done in that method.

Cor

"Cor Ligthert [MVP]" <no************@planet.nlschreef in bericht
news:ux****************@TK2MSFTNGP03.phx.gbl...

1. Regarding the memoryStream, if f1() returns memoryStream, and f2()
uses it, (and it can happen 1000 times during my program,) I better
close the memoryStream, so it will be garbage-collected soon, but I do
not have to set it to Nothing???
Correct
>Public Sub f2()
Dim ms As System.IO.MemoryStream = f1(...)
. . . ms.ReadByte() . . .
ms.Close()
End Sub

2. Regarding the xmlDocument - how do I force it to be garbage-collected
as soon as it is not used?
(at the beginning of my program, I read ~40 xml files, their total file
size is ~8M, but when creating from each xml file xmlDocument object, I
guess their size is larger)
Set them simple in a method, as soon as that goes out of scope and there
is no reference anymore to it from somewhere else, it simply is in the
normal procedure to be garbaged as soon as this is needed.

Forcing the GC has never been a success and as long as there is any
reference to an object, it is only doing a costly job in not wanted time.

Cor
>Thanks,

Atara.

Thanks,

Atara.

*** Sent via Developersdex http://www.developersdex.com ***


Jun 27 '08 #9
On Wed, 25 Jun 2008 23:59:44 -0700, Atara <At***@DD.comwrote:
>
Please correct me if I mis-understood you:

1. Regarding the memoryStream, if f1() returns memoryStream, and f2()
uses it, (and it can happen 1000 times during my program,) I better
close the memoryStream, so it will be garbage-collected soon, but I do
not have to set it to Nothing???
You need to call Close, but not so that it will be garbage collected
soon. Calling Close has no effect on garbage collection. You need to
call Close so that the object will release any resources it is
holding.
>
Public Sub f2()
Dim ms As System.IO.MemoryStream = f1(...)
. . . ms.ReadByte() . . .
ms.Close()
End Sub

2. Regarding the xmlDocument - how do I force it to be garbage-collected
as soon as it is not used?
(at the beginning of my program, I read ~40 xml files, their total file
size is ~8M, but when creating from each xml file xmlDocument object, I
guess their size is larger)
Once there is no longer a reference to the object, either because you
set the reference to Nothing or because the reference went out of
scope, the object is eligible for garbage collection. You could force
a garbage collection, but that is very bad practice. Just let the
garbage collector run when it determines it needs to run.
Jun 27 '08 #10


Thank you Jack and Cor.

Atara

*** Sent via Developersdex http://www.developersdex.com ***
Jun 29 '08 #11

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

Similar topics

3
by: grs | last post by:
I am using some Microsoft examples that: 1. Serialize an object using XmlSerializer and write a file out to the harddrive. 2. Read back in the file using XmlDocument.Load and populate a string. ...
2
by: Mike Brearley | last post by:
I need to write a script that will check a catch-all mailbox (pop3) and send a non delivery report back to the sender of the email. Background info: I have a domain hosted on a site that offers...
2
by: Chris Puncher | last post by:
Hi. I have a RCW class that was generated by the VS.NET2003 IDE when I added a COM dll reference to my project. This all works fine when I call methods on it. My initial problem is that in...
0
by: Sathya | last post by:
Hi, I have a vb.net webservice which takes XMLDocument as a parameter and returns bool. Signature of the method is some thing like this AddXmlEmpDetails(xmlEmp as XMLDocument) as Boolean. I...
4
by: saish | last post by:
Hello Need your help, I have a C++ win32 dll which takes xml document type by reference. I need to call this dll from c# .net. I tried using DllImport, but dll funtion when call does not...
3
by: rasx | last post by:
I get this error when I fail to ‘clean up’ my UTF-8 MemoryStream: “Invalid at the top level of the document. Error processing resource…” Visual Studio 2005 in debug mode shows well formed...
3
by: Yehia A.Salam | last post by:
Hello, I am trying to read from an xml file and put it to a memory stream so I can read it multiple times from the beginning using XmlTextReader without accessing the harddisk , I tried using...
1
by: shapper | last post by:
Hello, For the past hours I have been trying to solve a problem which is driving me crazy. I have to different codes where the problem to solve is the same: CODE 1 (Transforms a XML...
10
by: tshad | last post by:
I have a Dll I created in VS 2000. The namespace is MyFunctions and the Class is CryptoUtil. I have a program that is using the Class but it can't access it directly. I have a class (below)...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
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,...
0
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...
0
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 projectplanning, coding, testing,...

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.