idisposable

How to find all Classes implemeting IDisposable?

I am working on a large project, and one of my tasks is to remove possible memory leaks. In my code, I have noticed several IDisposable items not being disposed of, and have fixed that. However, that leads me to a more basic question, how do I find all classes used in my project that implement IDisposable? (Not custom-created classes but...

Passing IDisposable objects through constructor chains

I've got a small hierarchy of objects that in general gets constructed from data in a Stream, but for some particular subclasses, can be synthesized from a simpler argument list. In chaining the constructors from the subclasses, I'm running into an issue with ensuring the disposal of the synthesized stream that the base class constructor...

What happens to an IDisposable object after I return it?

I have a C# function that flips the orientation of a DataSet: static DataSet FlipDataSet(DataSet my_DataSet) { using (DataSet ds = new DataSet()) { foreach (DataTable dt in my_DataSet.Tables) { DataTable table = new DataTable(); for (int i = 0; i <= dt.Rows.Coun...

Combining foreach and using

I'm iterating over a ManageObjectCollection.( which is part of WMI interface). However the important thing is, the following line of code. : foreach (ManagementObject result in results) { //code here } The point is that ManageObject also implements IDisposable, so I would like to put "result" variable in a using block. Any idea o...

Disposing the members that implement IDisposable.

In my Dispose methods (like the one below), everytime i want to call someObj.Dispose() i also have a check for someObj!=null. Is that because of bad design on my part? Is their a cleaner way to ascertain that Dispose of all the members (implementing IDisposable) being used in an object is called without having a risk of NullReference e...

What's the purpose of GC.SuppressFinalize(this) in Dispose() method?

I have code that looks like this: /// <summary> /// Dispose of the instance /// </summary> public void Dispose() { if (_instance != null) { _instance = null; // Call GC.SupressFinalize to take this object off the finalization // queue and prevent finalization code for this object from // executi...

Method returns an IDisposable - Should I dispose of the result, even if it's not assigned to anything?

This seems like a fairly straightforward question, but I couldn't find this particular use-case after some searching around. Suppose I have a simple method that, say, determines if a file is opened by some process. I can do this (not 100% correctly, but fairly well) with this: public bool IsOpen(string fileName) { try { File...

The cost of finalize in .Net

(1) I've read a lot of questions about IDisposable where the answers recommend not using Finalize unless you really need to because of the process time involved. What I haven't seen is how much this cost is and how often it's paid. Every millisecond? second? hour, day etc. (2) Also, it seems to me that Finalize is handy when its not alw...

IEnumerator: Is it normal to have an empty Dispose method?

I'm writing an IEnumerator<T> class to iterate over a COM collection I'm wrappering. I've noticed that IEnumerator<T> extends IDisposable, so I'm required to implement the Dispose method. However, I can't think of anything I would put there, as I only have a reference to the collection (which I wouldn't want being disposed at the end...

Why is 'using' improving C# performances

It seems that in most cases the C# compiler could call Dispose() automatically. Like most cases of the using pattern look like: public void SomeMethod() { ... using (var foo = new Foo()) { ... } // Foo isn't use after here (obviously). ... } Since foo isn't used (that's a very simple detection) and si...

Should a managed class that wraps a DirectSound interface be IDisposable?

I'm writing a managed wrapper around DirectSound. (It's a simple partial wrapper that solves my specific problem and nothing more. Don't tell me about NAudio or whatever.) Should a managed class that wraps IDirectSound8 be IDisposable and why? Same question about IDirectSoundBuffer8. ...

How do I dispose my filestream when implementing a file download in ASP.NET?

I have a class DocumentGenerator which wraps a MemoryStream. So I have implemented IDisposable on the class. I can't see how/where I can possibly dispose it though. This is my current code, which performs a file download in MVC: using (DocumentGenerator dg = DocumentGenerator.OpenTemplate(path)) { /* some document manipulation wit...

How do you close an application when some WaitHandle is in the middle of a call to WaitOne?

Is there a standard way to close out an application "cleanly" while some WaitHandle objects may be in the state of a current blocking call to WaitOne? For example, there may be a background thread that is spinning along in a method like this: while (_request.WaitOne()) { try { _workItem.Invoke(); } finally {...

How to dispose when application crashes

Hi, got a issue: I create singleton helper object that wraps PerformanceCounter objects. It implements IDisposable... But now I have spotted that when I close my test sample console host application, counters are still visible in perfmon tool (in production I will be hosted in Windows Service) , and are still running. I figured out th...

Resharper custom search pattern to warn IDisposable objects

Since resharper still doesn't give out any warning regarding objects implementing IDisposable, I'd like to create some custom search patterns available in resharper 5.0. So far I have this: (And don't mind my replace comments in the patterns, I don't really care about it, I just want a clear warning in the code when dealing with dispos...

Disposing an IDisposable in Linq

(This is a follow on from a comment on an answer to this question) 18 months after I posted it, someone spotted a bug in one of my Linq examples, where I use an IDisposable half way through a method chain, which never gets disposed. I attempted to write an extension method to handle this: public static IEnumerable<R> Using<T, R>( ...

NUnit tests: Is IDisposable guaranteed if a non-expected exception is thrown?

If I create an IDisposable during the TestFixtureSetup of an NUnit test, and the test throws an unanticipated exception (e.g. external resource fails), will the IDisposable's Dispose() get called? Added>> If not, does NUnit provide guaranteed execution of TestFixtureTearDown or somewhere else that can be used for cleanup? ...

How does this class implement IDisposable if it doesn't have a Dispose method?

FtpWebResponse implements IDisposable, but it doesn't have a Dispose method. How is that possible? ...

Why would a class implement IDisposable explicitly instead of implicitly?

I was using the FtpWebResponse class and didn't see a Dispose method. It turns out that the class implements IDisposable, but does so explicitly so that you must first cast your instance to IDisposable before calling Dispose: // response is an instance of FtpWebResposne ((IDisposable) response).Dispose(); Why would the designer of a ...

WMPLib.dll not running Dispose() correctly. How can I play mp3s on Windows Mobile?

Using the Interop.WMPLib.dll to play mp3 files on Windows Mobile 6.x, but I can't get the GC to cleanly dispose of itself. I need to play a short mp3 (20-30 seconds) every 5 minutes for a long-running app (one hour or more), so I cannot afford to not have the GC dispose of the lib correctly. One solution was discussed on SO (http://stack...