dispose

When to dispose and why?

泪湿孤枕 提交于 2019-11-27 23:49:13
问题 I asked a question about this method: // Save an object out to the disk public static void SerializeObject<T>(this T toSerialize, String filename) { XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType()); TextWriter textWriter = new StreamWriter(filename); xmlSerializer.Serialize(textWriter, toSerialize); textWriter.Close(); } in the response I got this as an added remark: Make sure you always dispose disposable resources such as streams and text readers and writers. This

Minimal IDisposable implimenation for managed resources only

旧时模样 提交于 2019-11-27 23:42:48
There is a LOT of info around about the "standard full" IDisposable implementation for disposing of unmanaged resources - but in reality this case is (very) rare (most resources are already wrapped by managed classes). This question focuses on a mimimal implementation of IDisposable for the much more common "managed resources only" case. 1: Is the mimimal implementation of IDisposable in the code below correct, are there issues? 2: Is there any reason to add a full standard IDisposable implementation ( Dispose() , Dispose(bool) , Finalizer etc) over the minimal implimentation presented? 3: Is

How do I prevent a form object from disposing on close?

こ雲淡風輕ζ 提交于 2019-11-27 23:14:51
问题 I am using an MDIParent Form. When I close its child, the object of the child disposes. Is there a way to set child visibility to false instead of disposing? 回答1: By default, when you close a form, it will be disposed. You have to override the Closing event to prevent it, for example: // Use this event handler for the FormClosing event. private void MyForm_FormClosing(object sender, FormClosingEventArgs e) { this.Hide(); e.Cancel = true; // this cancels the close event. } 回答2: You can cancel

Does “using” statement always dispose the object?

孤街醉人 提交于 2019-11-27 23:03:54
问题 Does the using statement always dispose the object, even if there is a return or an exception is thrown inside it? I.E.: using (var myClassInstance = new MyClass()) { // ... return; } or using (var myClassInstance = new MyClass()) { // ... throw new UnexplainedAndAnnoyingException(); } 回答1: Yes, that's the whole point. It compiles down to: SomeDisposableType obj = new SomeDisposableType(); try { // use obj } finally { if (obj != null) ((IDisposable)obj).Dispose(); } Be careful about your

When is Dispose necessary?

柔情痞子 提交于 2019-11-27 22:14:46
When you have code like: Bitmap bmp = new Bitmap ( 100, 100 ); Graphics g = Graphics.FromImage ( bmp ); Pen p = new Pen ( Color.FromArgb ( 128, Color.Blue ), 1 ); Brush b = new SolidBrush ( Color.FromArgb ( 128, Color.Blue ) ); g.FillEllipse ( b, 0, 0, 99, 99 ); g.FillRegion ( b, pictureBox1.Region ); pictureBox1.BackColor = Color.Transparent; pictureBox1.Image = bmp; Do you have to dispose the pen and brush? What about bmp and the g? My main question is, if these were to be disposed manually, why don't they get disposed as soon as they get out of the scope? Is that what would happen, if you

Safely disposing Excel interop objects in C#?

青春壹個敷衍的年華 提交于 2019-11-27 22:13:56
i am working on a winforms c# visual studio 2008 application. the app talks to excel files and i am using Microsoft.Office.Interop.Excel; to do this. i would like to know how can i make sure that the objects are released even when there is an error? here's my code: private void button1_Click(object sender, EventArgs e) { string myBigFile=""; OpenFileDialog openFileDialog1 = new OpenFileDialog(); DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog. if (result == DialogResult.OK) // Test result. myBigFile=openFileDialog1.FileName; Excel.Application xlApp; Excel.Workbook

service.close() vs. service.abort() - WCF example

北城余情 提交于 2019-11-27 20:35:28
问题 In one of the WCF tutorials, I saw the following sample code: Dim service as ...(a WCF service ) try .. service.close() catch ex as Exception() ... service.abort() end try Is this the correct way to ensure that resources (i.e. connections) are released even under error conditions? 回答1: I've had good luck with this model: Dim service As New MyService() Dim closed As Boolean = False Try service.Open() If Not service.State = ServiceModel.CommunicationState.Opened Then ''Handle a not-opened state

Understanding Streams and their lifetime (Flush, Dispose, Close)

ⅰ亾dé卋堺 提交于 2019-11-27 20:30:17
问题 Note: I've read the following two questions already: Can you explain the concept of streams? C# using streams I'm coding in C# In almost all code samples that use streams, .Dispose(), .Flush(), .Close() are almost always called. In the concept of a stream, what does accomplish? If I don't dispose a stream that I stored in a variable, is my application leaking somewhere? Why do I need to call any of these functions? I've seen code samples that don't do this and still get the job done (without

How to abort socket's BeginReceive()?

廉价感情. 提交于 2019-11-27 20:22:58
Naturally, BeginReceive() will never end if there's no data. MSDN suggests that calling Close() would abort BeginReceive() . However, calling Close() on the socket also performs a Dispose() on it, as figured out in this great ansewr , and consequently EndReceive() would throw an exception because the object is already disposed (and it does!). How should I proceed? It seems like this is by (the very dumb) design. You must have this exception thrown and caught in your code. MSDN looks silent about it indeed, but if you look at the documentation of another asynchronous socket method, BeginConnect

Disposable singleton in C#

流过昼夜 提交于 2019-11-27 19:52:40
I have a singleton that uses the "static readonly T Instance = new T();" pattern. However, I ran into a case where T is disposable, and actually needs to be disposed for unit tests. How can I modify this pattern to support a disposable singleton? The interface I would like is something like: var x = Foo.Instance; var y = Foo.Instance; // x == y ... x.Release(); // this causes the next Foo.Instance to return a fresh object // also, it assumes no further operations on x/y will be performed. Note - the pattern has to be thread-safe, of course. Edit - for the purpose of production code, this is a