dispose

Finalizers with Dispose() in C#

社会主义新天地 提交于 2019-11-30 18:26:35
问题 See the code sample from MSDN: (http://msdn.microsoft.com/en-us/library/b1yfkh5e(v=VS.100).aspx) // Design pattern for a base class. public class Base: IDisposable { private bool disposed = false; //Implement IDisposable. public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { // Free other state (managed objects). } // Free your own state (unmanaged objects). // Set large fields to null. disposed =

How to handle exception thrown from Dispose?

空扰寡人 提交于 2019-11-30 17:30:37
Recently, I was researching some tricky bugs about object not disposed. I found some pattern in code. It is reported that some m_foo is not disposed, while it seems all instances of SomeClass has been disposed. public class SomeClass: IDisposable { void Dispose() { if (m_foo != null) { m_foo.Dispose(); } if (m_bar != null) { m_bar.Dispose(); } } private Foo m_foo; private Bar m_bar; } I suspects that Foo.Dispose might throw a exception, so that following code is not executed so m_bar is not disposed. Since Foo/Bar might be from third party, so it is not guaranteed to not throwing exception. If

Should Dispose methods be unit tested?

僤鯓⒐⒋嵵緔 提交于 2019-11-30 17:18:30
I am using C#. Is it advised to unit test dispose methods? If so why, and how should one test these methods? Yes, but it might be hard. There are two things that can generally happen in Dispose implementation: Unmanaged resources are released. In this case it's pretty hard to verify that the code called, for example, Marshal.Release . A possible solution is to inject an object that can do the disposing and pass a mock to it during testing. Something to this effect: interface ComObjectReleaser { public virtual Release (IntPtr obj) { Marshal.Release(obj); } } class ClassWithComObject :

MVC 3 - The ObjectContext instance has been disposed and can no longer be used for operations that require a connection

十年热恋 提交于 2019-11-30 17:12:55
问题 I'm very new to C# and MVC in general and I've been creating my own little blog site as a test project. Although most things are working up to this point, I have had problems selecting multiple columns from LINQ queries. It was only after stumbling on a question on SO that I realised I could use the generated entities classes as strongly-typed models to handle this. I've needed this as I've been creating a database layer (which is also something I've not used before) and trying to pass

How do I dispose of resources after an ASP.NET Web API request completes?

*爱你&永不变心* 提交于 2019-11-30 16:11:30
I am attempting to expose an IQueryable<> over ASP.NET Web API and I find that I need to keep the data source open until the request completes, so that the OData query system built into ASP.NET Web API can do its job. OK, that sounds perfectly reasonable. But where do I dispose of the data source? I do not see any obvious place for this. Should I manage request state in the Application? What is the standard way to do this? Is the Dispose() method of the controller the appropriate place? I.e. is there a guarantee that one controller instance serves only one request or is that just an

Handling with temporary file stream

怎甘沉沦 提交于 2019-11-30 14:41:24
Say I want to define a TempFileStream class that creates a temporary file using Path.GetTempFileName() method. A temporary file must be deleted when TempFileStream's object is no longer needed, e.g. closed or disposed: class TempFileStream: FileStream { string m_TempFileName = Path.GetTempFileName(); public TempFileStream(FileMode fileMode): base(m_TempFileName,fileMode) {} /// ... public ovverride Dispose(bool disposing) { /// ??? } } How should I implement this simply and safely? Try this one instead: public class TempFileStream : FileStream { public TempFileStream() : base(Path

How to dispose asynchronously?

牧云@^-^@ 提交于 2019-11-30 11:04:12
Let's say I have a class that implements the IDisposable interface. Something like this: MyClass uses some unmanaged resources, hence the Dispose() method from IDisposable releases those resources. MyClass should be used like this: using ( MyClass myClass = new MyClass() ) { myClass.DoSomething(); } Now, I want to implement a method that calls DoSomething() asynchronously. I add a new method to MyClass : Now, from the client side, MyClass should be used like this: using ( MyClass myClass = new MyClass() ) { myClass.AsyncDoSomething(); } However, if I don't do anything else, this could fail as

How do I add Dispose functionality to a C# UserControl?

本秂侑毒 提交于 2019-11-30 10:53:41
问题 I have a class which implements UserControl. In .NET 2005, a Dispose method is automatically created in the MyClass.Designer.cs partial class file that looks like this: protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } If I want to add my own Dispose functionality, where would I put it? Since this file is generated, I don't want to add code here and risk it getting blown away. 回答1: In such a case I

Finalizers and Dispose

混江龙づ霸主 提交于 2019-11-30 10:34:18
I've got a class named BackgroundWorker that has a thread constantly running. To turn this thread off, an instance variable named stop to needs to be true . To make sure the thread is freed when the class is done being used, I've added IDisposable and a finalizer that invokes Dispose() . Assuming that stop = true does indeed cause this thread to exit, is this sippet correct? It's fine to invoke Dispose from a finalizer, right? Finalizers should always call Dispose if the object inherits IDisposable , right? /// <summary> /// Force the background thread to exit. /// </summary> public void

Memory Leak questions

南楼画角 提交于 2019-11-30 09:34:22
问题 I've been reading a lot about this since I've been asked to fix a C# application that has memory leaking problems, but I haven't found an answer for these 2 issues: Consider the following code: private static ArrayList list = new ArrayList(); public void Function() { list.add(object1); list.add(object2); //didn't call clear() prior to reusing list list = new ArrayList(); } Since the list wasn't cleared before creating a new one, will this generate some sort of garbage that won't be released