473,396 Members | 1,775 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.

closing and disposing

I came across the following code snippet in a project
I inherited. Having 2 close() and 2 dispose() calls for
essentially one stream seems to me to be way too much
overkill.

What is the proper idiom for this?

MemoryStream memStream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(memStream);
// ...use writer
writer.Close();
byte[] data = memStream.ToArray();
memStream.Close();
((IDisposable)writer).Dispose();
((IDisposable)memStream).Dispose();
return data;
Thanks,
Keith

Nov 17 '05 #1
5 2323
In fact all you need to do is close the file, and any one of the four calls
will do that.

The best way to do this, however is to use C#'s using construction...
using (MemoryStream memStream = new MemoryStream())
{
BinaryWriter writer = new BinaryWriter(memStream);
// ...use writer
byte[] data = memStream.ToArray();
return data;
}

The way you had it, if any of the code in
// ...use writer
threw an exception, the file would not get closed... if you use the "using"
construction, it puts all the enclosed into a try/finally block for you, and
then calls Dispose() in the finally block, regardless of whether or not any
exceptions were thrown.

"keithv" wrote:
I came across the following code snippet in a project
I inherited. Having 2 close() and 2 dispose() calls for
essentially one stream seems to me to be way too much
overkill.

What is the proper idiom for this?

MemoryStream memStream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(memStream);
// ...use writer
writer.Close();
byte[] data = memStream.ToArray();
memStream.Close();
((IDisposable)writer).Dispose();
((IDisposable)memStream).Dispose();
return data;
Thanks,
Keith

Nov 17 '05 #2

CBretana wrote:
In fact all you need to do is close the file, and any one of the four calls will do that.

The best way to do this, however is to use C#'s using construction...
using (MemoryStream memStream = new MemoryStream())
{
BinaryWriter writer = new BinaryWriter(memStream);
// ...use writer
byte[] data = memStream.ToArray();
return data;
}


So you saying you don't need to explicitly close or dispose the
BinaryWriter class? I guess then that BinaryWriter doesn't do
any buffering?

Keith

Nov 17 '05 #3
The only reason to call Close or dispose is to "clean up" external resources
which might not get properly disposed of when the Garbage Collector
"Collects" the instance of the object. The only EXTERNAL resource the Stream
writer or the Stream deals with is the file... ANy buffers used internally
are part of the CLR instance, and will be released when the instance is
"collected" by the GC..

"keithv" wrote:

CBretana wrote:
In fact all you need to do is close the file, and any one of the four

calls
will do that.

The best way to do this, however is to use C#'s using construction...
using (MemoryStream memStream = new MemoryStream())
{
BinaryWriter writer = new BinaryWriter(memStream);
// ...use writer
byte[] data = memStream.ToArray();
return data;
}


So you saying you don't need to explicitly close or dispose the
BinaryWriter class? I guess then that BinaryWriter doesn't do
any buffering?

Keith

Nov 17 '05 #4
Actually he is saying you do need to close or dispose, but by doing:

using (MemoryStream memStream = new MemoryStream())
{
} <-- dispose is called for you at this point. So you yourself don't need to
call dispose/close

--
Thanks
Wayne Sepega
Jacksonville, Fl
"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein

"keithv" <kv*****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

CBretana wrote:
In fact all you need to do is close the file, and any one of the four

calls
will do that.

The best way to do this, however is to use C#'s using construction...
using (MemoryStream memStream = new MemoryStream())
{
BinaryWriter writer = new BinaryWriter(memStream);
// ...use writer
byte[] data = memStream.ToArray();
return data;
}


So you saying you don't need to explicitly close or dispose the
BinaryWriter class? I guess then that BinaryWriter doesn't do
any buffering?

Keith

Nov 17 '05 #5
Wayne wrote:
Actually he is saying you do need to close or dispose, but by doing:

using (MemoryStream memStream = new MemoryStream())
{
} <-- dispose is called for you at this point.
So you yourself don't need to call dispose/close


Actually, my question was about explicitly closing/disposing a
BinaryWriter class that wraps the MemoryStream which you edited out.

Here's the unelided code snippet:

uing (MemoryStream memStream = new MemoryStream())
BinaryWriter writer = new BinaryWriter(memStream);
// ...use writer
byte[] data = memStream.ToArray();
return data;
}

So, again two questions:
1) do I need to explicitly close the BinaryWriter here?
Or, more generally, if you chain a bunch of Stream clases
does closing any one of them close them all? (If so, do
all the dispose() get called regardless of who gets the
first dispose call?)

2) for stream classes, are close and dispose equivalent such
that which gets called is stylistic question not a semantic
one?

Thanks
Keith

Nov 17 '05 #6

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

Similar topics

16
by: ThunderMusic | last post by:
Hi, My app does not stop whan I click on the form. I mean, the form is closing, but the process keeps running in the task manager. So I figured there are memory leaks or some object's process...
1
by: C Sharp beginner | last post by:
I'm sorry about this verbose posting. This is a follow-up to my yesterday's posting. Thanks William for your reply. I understand it is a good practice to open connections as late as possible and...
0
by: Andrew | last post by:
I get a Null Reference Exception if I close a modeless form (that is, a form displayed using Show()) when a selection is made from a ComboxBox. If the form is modal (displayed using ShowDialog())...
3
by: Andrew | last post by:
I get a Null Reference Exception if I close a non-modal dialog (that is, a form opened with Show()) when a selection is made from a ComboBox. The error message refers to Unsafe Native Methods, but...
1
by: MuZZy | last post by:
HI, I have a user control, say with one textBox. I need the following: when the user closes form (either calling Form.Close, or pressing "X" or anyhow), UserControl copies a file from a...
1
by: Cedric | last post by:
Hello, I have a form that could be closed by a code keyed or selected in a combobox but when I select my code my program end up with an error (NullReferenceException) and not when I keying the...
5
by: sherifffruitfly | last post by:
Hi all, I'm certain this is stupid, but I'd like to know how to keep a form open for, say, longer than 1ms. Here's the code I'm using to open my form: TestModelFileReaderForm modelInfo = new...
5
by: zacks | last post by:
If I close and dispose an ODBCConnection object, shouldn't the connection actually close? I have found that even after closing and disposing an ODBCConnection, the database it was connected to...
19
by: rbrowning1958 | last post by:
Hello, I am confused by dispose etc. and hope someone can set me right. 1. The Dispose(Bool) the IDE generates for a form has nothing to do with IDisposable, right? 2. So when is this called?...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
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
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...

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.