idisposable

Will the GC call IDisposable.Dispose for me?

The .NET IDisposable Pattern implies that if you write a finalizer, and implement IDisposable, that your finalizer needs to explicitly call Dispose. This is logical, and is what I've always done in the rare situations where a finalizer is warranted. However, what happens if I just do this: class Foo : IDisposable { public void Dis...

How to do C++ style destructors in C#?

I've got a C# class with a Dispose function via IDisposable. Its intended to be used inside a using block so the expensive resource it handles can be released right away. Problem is that a bug occurred when an exception was thrown before Dispose was called, and the programmer neglected to use using or finally. <rant> In C++, I never ha...

Calling base.Dispose() automatically from derived classes

Edit - New Question Ok lets rephrase the question more generically. Using reflection, is there a way to dynamically call at runtime a base class method that you may be overriding. You cannot use the 'base' keyword at compile time because you cannot be sure it exists. At runtime I want to list my ancestors methods and call the ancestor...

VB.NET - Should a Finalize method be added when implementing IDisposable?

In Visual Studio, when I type the line "Implements IDisposable", the IDE automatically adds: a disposedValue member variable a Sub Dispose() Implements IDisposable.Dispose a Sub Dispose(ByVal disposing As Boolean) The Dispose() should be left alone, and the clean up code should be put in Dispose(disposing). However the Dispose Final...

Finalizers and Dispose

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 ...

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

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. ...

Intercepting an exception inside IDisposable.Dispose

In the IDisposable.Dispose method is there a way to figure out if an exception is being thrown? using (MyWrapper wrapper = new MyWrapper()) { throw new Exception("Bad error."); } If an exception is thrown in the using statement I want to know about it when the IDisposable object is disposed. ...

Winforms Disposing IDisposable

Does anyone have a good articles or tutorial on correctly using dispose and IDisposable. I am trying to explain this to some junior dev and wanted to get some extra material, examples, etc. ...

How to unit test IDisposable?

I'm working on a piece of library code around IDisposable. The managed path (via using) is easily testable. I'm wondering about the finalizer though: Is calling System.GC.Collect() sufficient to force the finalizer to run? ...

When can I dispose an IDisposable WPF control e.g. WindowsFormsHost?

The WPF control WindowsFormsHost inherits from IDisposable. If I have a complex WPF visual tree containing some of the above controls what event or method can I use to call IDispose during shutdown? ...

Dealing with .NET IDisposable objects

I work in C#, and I've been pretty lax about using using blocks to declare objects that implement IDisposable, which you're apparently always supposed to do. However, I don't see an easy way of knowing when I'm slipping up. Visual Studio doesn't seem to indicate this in any way (am I just missing something?). Am I just supposed to che...

NDepend CQL Query for missing IDisposable implementation

I realize that the query this question is looking for won't be enough to find every little problem with IDisposable implementations, but every early warning counts, so I'll take what I can get. I'd like to know if anyone has come up with a CQL query for NDepend that will list all classes that doesn't implement IDisposable, but has one o...

How do you dispose of an IDisposable in Managed C++

I'm trying to Dispose of an IDisposable object(FileStream^ fs) in managed C++ (.Net 2.0) and am getting the error 'Dispose' : is not a member of 'System::IO::FileStream' It says that I should invoke the destructor instead. Will calling fs->~FileStream(); call the dispose method on the FileStream object? Why can't I call Dispose? ...

Unregistered event handlers cause memory leak

I'm maintaining a web application that has a memory leak. Based on my investigation using Red Gate ANTS memory profiler I'm pretty sure that the memory leak is caused by event handlers in the business layer. There's a collection that registers an event handler on each item that's added so that the collection can re-sort when the item's...

IDisposable Winform

What is the best practice for implementing IDisposable on a Winform? I have a dialog which extends System.Windows.Forms.Form The generated designer.cs already contains an implementation of the virtual Dispose(bool) method My form has a field added manually which implements IDisposable Ideally I would be able to hook into the Dispos(b...

Using IDisposable to unsubscribe events

I have a class that handles events from a WinForms control. Based on what the user is doing, I am deferencing one instance of the class and creating a new one to handle the same event. I need to unsubscribe the old instance from the event first - easy enough. I'd like to do this in a non-proprietary manner if possible, and it seems like ...

What's the purpose of implementing the IDisposable interface?

What's the purpose of implementing the IDisposable interface? I've seen some classes implementing it and I don't understand why. ...

Best practice: Override OnDispose(bool disposing) vs Disposed event on Component.

In .Net the Component class exposes a Disposed event. It also provides a protected member OnDispose(bool disposing). What is the best practice for a custom component that extends Component? Override OnDispose(bool) or attach an event handler to Disposed on construction? My feeling is that one should override OnDispose(bool) and sea...

How does LINQ defer execution when in a using statement

Imagine I have the following: private IEnumerable MyFunc(parameter a) { using(MyDataContext dc = new MyDataContext) { return dc.tablename.Select(row => row.parameter == a); } } private void UsingFunc() { var result = MyFunc(new a()); foreach(var row in result) { //Do something } } According to the do...

Singleton with finalizer but not IDisposable

This is what I understand about IDisposable and finalizers from "CLR via C#", "Effective C#" and other resources: IDisposable is for cleaning up managed and unmanaged resources deterministically. Classes that are responsible for unmanaged resources (e.g. file handles) should implement IDisposable and provide a finalizer to guarantee th...