dispose

Proper cleanup of WPF user controls

╄→尐↘猪︶ㄣ 提交于 2019-11-29 02:39:07
问题 I am relatively new to WPF, and some things with it are quite foreign to me. For one, unlike Windows Forms, the WPF control hierarchy does not support IDisposable. In Windows Forms, if a user control used any managed resources, it was very easy to clean up the resources by overriding the Dispose method that every control implemented. In WPF, the story is not that simple. I have searched for this for several hours, and encountered two basic themes: The first theme is Microsoft clearly stating

What's the purpose of the components IContainer generated by the Winforms designer?

断了今生、忘了曾经 提交于 2019-11-29 02:14:14
问题 When you create a new form in Visual Studio, the designer generates the following code in the .Designer.cs file: /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) {

Declare IDisposable for the class or interface?

前提是你 提交于 2019-11-29 02:06:56
问题 Starting from the follwing situation: public interface ISample { } public class SampleA : ISample { // has some (unmanaged) resources that needs to be disposed } public class SampleB : ISample { // has no resources that needs to be disposed } The class SampleA should implement the interface IDisposable for releasing resources. You could solve this in two ways: 1. Add the required interface to the class SampleA: public class SampleA : ISample, IDisposable { // has some (unmanaged) resources

Any sense to set obj = null(Nothing) in Dispose()?

自古美人都是妖i 提交于 2019-11-29 01:01:02
Is there any sense to set custom object to null ( Nothing in VB.NET) in the Dispose() method? Could this prevent memory leaks or it's useless?! Let's consider two examples: public class Foo : IDisposable { private Bar bar; // standard custom .NET object public Foo(Bar bar) { this.bar = bar; } public void Dispose() { bar = null; // any sense? } } public class Foo : RichTextBox { // this could be also: GDI+, TCP socket, SQl Connection, other "heavy" object private Bitmap backImage; public Foo(Bitmap backImage) { this.backImage = backImage; } protected override void Dispose(bool disposing) { if

How does one tell if an IDisposable object reference is disposed?

只愿长相守 提交于 2019-11-29 00:48:13
Is there a method, or some other light-weight way, to check if a reference is to a disposed object? P.S. - This is just a curiousity (sleep well, not in production code). Yes, I know I can catch the ObjectDisposedException upon trying to access a member of the object. It depends, there are IDisposable objects that allow to call the Dispose method as much as you want, and there are IDisposable objects that throw ObjectDisposedException . In such a case these objects must track the state (usually implemented with a private boolean field isDisposed ). No - default implementation of IDisposable

“Object can be disposed of more than once” error

[亡魂溺海] 提交于 2019-11-28 21:04:20
When I run code analysis on the following chunk of code I get this message: Object 'stream' can be disposed more than once in method 'upload.Page_Load(object, EventArgs)'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object. using(var stream = File.Open(newFilename, FileMode.CreateNew)) using(var reader = new BinaryReader(file.InputStream)) using(var writer = new BinaryWriter(stream)) { var chunk = new byte[ChunkSize]; Int32 count; while((count = reader.Read(chunk, 0, ChunkSize)) > 0) { writer.Write(chunk, 0, count); } } I don't

How to check if object has been disposed in C# [duplicate]

≯℡__Kan透↙ 提交于 2019-11-28 20:00:24
Possible Duplicate: How does one tell if an IDisposable object reference is disposed? Is there a method to check if object has been disposed different then try { myObj.CallRandomMethod(); } catch (ObjectDisposedException e) { // now I know object has been disposed } In my case I'm using TcpClient class that has Close() method which disposes object and this can happen in piece of code I don't have control of. In this case I would like to have better solution then catching exception. Hans Passant A good way is to derive from TcpClient and override the Disposing(bool) method: class MyClient :

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

家住魔仙堡 提交于 2019-11-28 19:53:36
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? 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 here End If service.MyMethod() service.Close() closed = true Catch ex As Exception ''Handle errors here

Is there a situation in which Dispose won't be called for a 'using' block?

为君一笑 提交于 2019-11-28 19:03:40
This was a telephone interview question I had: Is there a time when Dispose will not be called on an object who's scope is declared by a using block? My answer was no - even if an exception happens during the using block, Dispose will still be called. The interviewer disagreed and said if using is wrapped in a try - catch block then Dispose will not be called by the time you enter the catch block. This goes contrary to my understanding of the construct, and I haven't been able to find anything that backs up the interviewers point of view. Is he correct or might I have misunderstood the

Do I need to consider disposing of any IEnumerable<T> I use?

妖精的绣舞 提交于 2019-11-28 18:05:15
It's recently been pointed out to me that various Linq extension methods (such as Where , Select , etc) return an IEnumerable<T> that also happens to be IDisposable . The following evaluates to True new int[2] {0,1}.Select(x => x*2) is IDisposable Do I need to dispose of the results of a Where expression? Whenever I call a method returning IEnumerable<T> , am I (potentially) accepting responsibility for calling dispose when I've finished with it? No, you don't need to worry about this. The fact that they return an IDisposable implementation is an implementation detail - it's because iterator