dispose

Is disposing this object, enough? or do i need to do more?

Hi folks. I have this code :- using (System.Security.Cryptography.SHA256 sha2 = new System.Security.Cryptography.SHA256Managed()) { .. } Do I need to put this line of code, just BEFORE I leave that dispose scope .. or does the dispose 'call' that already. sha2.Clear(); ...

What is the difference between using IDisposable vs a destructor in C#?

When would I implement IDispose on a class as opposed to a destructor? I read this article, but I'm still missing the point. My assumption is that if I implement IDispose on an object, I can explicitly 'destruct' it as opposed to waiting for the garbage collector to do it. Is this correct? Does that mean I should always explicitly ...

Unittesting methods that contain using statements

I am wondering if Unittesting and using statements can really go hand in hand as there is no way to mock the disposable object instantiated in the using statement. How would I be able to effectively unittest a method containing the following using statement? public void MyMethod() { using(MyDisposableClass disp = new MyDisposableCla...

Winforms: Is it safe to delete the code in the automatically created Dispose() method created by Visual Studio for Windows Forms

Background In Visual Studio 2008 Create a new Windows Forms Application, this will create a skeleton project with a class called "Form1". VS2008 automatically creates a Dispose() method. /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be di...

Properly disposing of a DbConnection

I have a class called DatabaseHelper that wraps a DbConnection. What's the proper way to setup this class for a using statement? I have implemented IDisposible, but I'm not sure when and where I should be calling Connection.Close() or Connection.Dispose(). When I simply call Connection.Dispose() in my own Dispose() method, I'll sometime...

Containment Tree ?

OK thought I understood IDipose but just reading the best practices section of Accelerated VB.NET and it states that "The object doesn't contain any objects that implement iDispose, so you don't need to iterate through the containment tree calling iDispose" Is this "containment tree" just what the program know's he has used and then dis...

Application.Run throws ArgumentException was unhandled

I have a condition in which I need to close the application and so I call this.Dispose() when I set a certian flag. At first I thought it was a problem of calling functions after I call this.Dispose() and so I moved the code to be the last thing called, but I still get a "ArgumentException was unhandled" "Parameter is not valid." on th...

How to dispose asynchronously?

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

What happens to an IDisposable object created in the middle of a statement that I cannot explicity call Dispose() on?

Say I'm working with Sharepoint (this applies to other object models as well) and in the middle of my statement, I call a method, in this case "OpenWeb()", which creates an IDisposable SPWeb object. Now, I cannot call Dispose() on the SPWeb object because I don't have the reference to it. So do I need to be concerned about this leaking ...

Problem disposing of socket / finalising twice?

I'm working with some code (not mine I hasten to add, I don't trust this much at all) for a class which opens a socket, makes requests and listens for responses, which is throwing an exception in a way I can't comprehend when tested in xunit. I assume the same exception happens "live" but the class is referenced by a singleton so it is p...

Do I need to dispose a web service reference in ASP.NET?

Does the garbage collector clean up web service references or do I need to call dispose on the service reference after I'm finished calling whatever method I call? ...

In the Dispose(bool) method implementation, Shouldn't one set members to null?

None of the guides/notes/articles that discuss IDisposable pattern suggest that one should set the internal members to null in the Dispose(bool) method (especially if they are memory hogging beasts). I've come to realize the importance of it while debugging an internal benchmark tool. What used to happen was that, there was this buffer ...

Disposing a static brush

Hi, I'm writing a biorhythm app. To test it i have a form with a Button and a PictureBox. When I click on the button i do myPictureBox.Image = GetBiorhythm2(); Which runs ok for the first time, but on the second click it causes the following exception: System.ArgumentException: Parameter is not valid. at System.Drawing.Graphics.Ch...

Is it necessary to dispose System.Timers.Timer if you use one in your application?

I am using System.Timers.Timer class in one of the classes in my application. I know that Timer class has Dispose method inherited from the parent Component class that implements IDisposable interface. Instances of the class below are created many times during my application lifecycle; each of them has an instance of Timer class that gen...

What's the point of overriding Dispose(bool disposing) in .NET?

If I write a class in C# that implements IDisposable, why isn't is sufficient for me to simply implement public void Dispose(){ ... } to handle freeing any unmanaged resources? Is protected virtual void Dispose(bool disposing){ ... } always necessary, sometimes necessary, or something else altogether? ...

Identify IDisposable objects

Hi, i have to review a code made by some other person that has some memory leaks. Right now i'm searching the disposable objects to enclause them with the using statement and i would like to know if there is a quick way that tells you all the disposable objects declared in. I mean something like resharper or another visual studio plugin....

WCF Custom ServiceHost Cleanup in IIS

I have a custom WCF ServiceHost that opens a persistent object to an external resource. The operations on the contract are then wired on to this persistent object. The persistent object manages itself to ensure it's always in a usable state. I'm having a difficult time deciding where it is safe to dispose this object -- because once it'...

How to guarantee a NotifyIcon disappears?

Hi, I'm using a NotifyIcon control in one of my child (modal) forms and it is working fine. SHowing balloon tips as expected, handling mouse events etc... It doesn't however vanish when I would expect it to. Specifically, when i exit the child form and the parent is back in control the icon still remains. It's tooltip is accessible so ...

When should I manually dispose of controls? How do I know if a control implements IDisposable?

In a previous question about ridding the system tray of an old NotifyIcon I was told that I should Dispose anything that implements IDisposable. Sounds like good practise to me however as a newbie it raises more questions :-) How do I know if a control implements IDisposable? Should I build a class that attempts to dispose everything ...

How do i know when i need to dispose an object?

HOW do i know when i need to dispose of something? Someone just mention i had several objects in my code that i need to dispose of. I had no idea i needed to dispose anything (this is my first week with C#). How do i know when i need to dispose an object? i was using http://msdn.microsoft.com/en-us/library/system.security.cryptography.ha...