473,406 Members | 2,713 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,406 software developers and data experts.

Do I need to call MemoryStream.Close ?

I use local to methods MemoryStreams.
Both read-only and read-write ones.

Do I need to call MemoryStream.Close for read-only MemoryStreams before
leaving the method ?

And what about read-write MemoryStreams ?
--
Best regards,

Oleg Subachev
su******@ural.ru
Nov 17 '05 #1
9 27535
Hi,

I suggest you to close / dispose your memory stream, and also set memsrm
variable to null..
In some cases if you dont set memsrm to null, it will lock this file and you
could not access it again till you restart your PC.. (maby this happends
only to me).

Regards,
Josip Habjan, Croatia
URL: www.habjansoftware.com

"Oleg Subachev" <ol**@urvb.ru> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I use local to methods MemoryStreams.
Both read-only and read-write ones.

Do I need to call MemoryStream.Close for read-only MemoryStreams before
leaving the method ?

And what about read-write MemoryStreams ?
--
Best regards,

Oleg Subachev
su******@ural.ru

Nov 17 '05 #2
> I suggest you to close / dispose your memory stream, and also set memsrm
variable to null..
In some cases if you dont set memsrm to null, it will lock this file and you could not access it again till you restart your PC.. (maby this happends
only to me).


My MemoryStreams are not linked with any files.
--
Best regards,

Oleg Subachev
su******@ural.ru
Nov 17 '05 #3
You should not have to set the variable to null. The only thing you
should need to do is call close/dispose. However, it's always a good
idea to set the variable to null so you know not to use it anymore.

Nov 17 '05 #4
Sorry .. eyes are faster then mind... :o)

Regards,
Josip Habjan, Croatia
URL: www.habjansoftware.com

"Oleg Subachev" <ol**@urvb.ru> wrote in message
news:eg*************@tk2msftngp13.phx.gbl...
I suggest you to close / dispose your memory stream, and also set memsrm
variable to null..
In some cases if you dont set memsrm to null, it will lock this file and

you
could not access it again till you restart your PC.. (maby this happends
only to me).


My MemoryStreams are not linked with any files.
--
Best regards,

Oleg Subachev
su******@ural.ru

Nov 17 '05 #5
Hi

It is good practice to close all the streams after using them. However,
MemoryStream is little bit different. Closing/Disposing this stream doesn't
do much resourcewise. It just marks the stream as closed so no more
reading/writing to the stream are possible. Methods like ToArray() and
GetBuffer() still works, which means that the data is still in the memory.
The Dispose simply calls Close so it doesn't make much of a difference.

Depending on the type of the variable that holds the reference to the stream
(whether it is a local variable or class field), the level of memory
consumptions and the structure of the code that uses this variable it make
become more important to set the variable to *null* than to close/dispose
the stream.

But anyways it is always a good programming practice to close/dispose all
the streams when finish working with them.


--
Stoitcho Goutsev (100) [C# MVP]

"BravesCharm" <ma*********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
You should not have to set the variable to null. The only thing you
should need to do is call close/dispose. However, it's always a good
idea to set the variable to null so you know not to use it anymore.

Nov 17 '05 #6
Oleg,

Some people in this thread have suggested that you set the variable to
null. You actually don't want to do this. In debug builds, you will end up
keeping the reference around longer (because of lack of optimizations) than
necessary. In release builds, assignments to null should be optimized out.

Also, it is good practice to always call Dispose on any instance of a
type that implements IDisposable.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Oleg Subachev" <ol**@urvb.ru> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
I use local to methods MemoryStreams.
Both read-only and read-write ones.

Do I need to call MemoryStream.Close for read-only MemoryStreams before
leaving the method ?

And what about read-write MemoryStreams ?
--
Best regards,

Oleg Subachev
su******@ural.ru

Nov 17 '05 #7


Nicholas Paldino [.NET/C# MVP] wrote:
Also, it is good practice to always call Dispose on any instance of a
type that implements IDisposable.


If you are the one "responsible" for that object. For example:

void foo(Stream s) {
s.Write(...);
// should usually *not* invoke s.Dispose();
}

whereas the creator of the object should (usually) either: Dispose() it
when done:

void bar() {
using ( MemoryStream s = new MemoryStream() ) {
foo(s);
...;
}
}

or be sure it has given that responsibility to some other code:

class Baz: IDispoable {
Stream s;
void Dispose() { if ( s != null ) s.Dispose(); }
}
void quux(Baz baz) {
baz.s = new MemoryStream();
}

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #8
Helge,

Yes, if you are responsible for that. Sorry, I assumed that was
implied, but thanks for the clarification.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Helge Jensen" <he**********@slog.dk> wrote in message
news:u6**************@TK2MSFTNGP14.phx.gbl...


Nicholas Paldino [.NET/C# MVP] wrote:
Also, it is good practice to always call Dispose on any instance of a
type that implements IDisposable.


If you are the one "responsible" for that object. For example:

void foo(Stream s) {
s.Write(...);
// should usually *not* invoke s.Dispose();
}

whereas the creator of the object should (usually) either: Dispose() it
when done:

void bar() {
using ( MemoryStream s = new MemoryStream() ) {
foo(s);
...;
}
}

or be sure it has given that responsibility to some other code:

class Baz: IDispoable {
Stream s;
void Dispose() { if ( s != null ) s.Dispose(); }
}
void quux(Baz baz) {
baz.s = new MemoryStream();
}

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-

Nov 17 '05 #9
Josip Habjan <jh*****@SPAM-OFF.net.hr> wrote:
I suggest you to close / dispose your memory stream, and also set memsrm
variable to null..
In some cases if you dont set memsrm to null, it will lock this file and you
could not access it again till you restart your PC.. (maby this happends
only to me).


1) MemoryStreams aren't associated with any files
2) Closing a memory stream doesn't actually do anything, although it's
good practice to get into the habit of disposing of all IDisposable
objects where appropriate (i.e. where you own it)
3) Setting a variable to null will have no effect if the stream has
already been disposed; it's very rarely worth setting a variable to
null for garbage collection reasons, to be frank.

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

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

Similar topics

4
by: Carolina | last post by:
Hi. I have a MDIchild form called Form1. In Form1_Load code, the form populates a Listview control based on a datareader, but I want to close the form inmediately and return to the parent form,...
5
by: Darius | last post by:
I'm writing here in hopes that someone can explain the difference between the new and virtual/override keywords in C#. Specifically, what is the difference between this: public class Window {...
1
by: keithv | last post by:
Hi, The msdn doc for MemoryStream has two conflicting statements about accessing a MemoryStream's buffer after it's been closed: The buffer is still available on a MemoryStream once the...
2
by: SQLScott | last post by:
For some reason I am drawing a blank on this, so I would be extremely greatful if someone could help me out. I am trying to get a MemoryStream out of a Byte array. How do I get it back out? ...
1
by: kaoz | last post by:
Hello, I have a the following class using the Thread class from Common C++ class CMyThread: public ost::Thread { public: CMyThread (); void run (); ....
2
by: OB | last post by:
I want to be able to press a button on the Browser page and close that browser window. Anybody has a sample for ASP.NET 2.0? Thanks OB
4
by: Harshapic | last post by:
I am looking out for a automated program/tool that creates call tree for code written specifically for Microchip C compiler. I tried many tools that failed with some language constructs specific to...
6
by: RFleming | last post by:
I am a pretty experienced VB programmer trying to make a jump to C#. I have created a simple (so I thought) project and seem to be stuck. I know I can create a form and write the code within the...
3
by: BASSPU03 | last post by:
Private Sub FilePath_Click() Rem Me! Dim stAppName As String Dim stlink As String stlink = Me! stAppName = "C:\Program Files\Internet Explorer\IEXPLORE.EXE...
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
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...
0
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
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,...
0
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...

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.