dispose

When would dispose method not get called?

I was reading this article the other day and was wondering why there was a Finalizer along with the Dispose method. I read here on SO as to why you might want to add Dispose to the Finalizer. My curiousity is, when would the Finalizer be called over the Dispose method itself? Is there a code example or is it based on something happeni...

VB.NET Memory Management

Update: I probably confused memory usage issues with the UI sharing same thread as the processing (as pointed out by MusiGenesis below). However regarding the Memory usage. I am still not able to find VB.net specific syntax, although people have pointed out some great .Net and C# information below (and if I were more versed in those te...

When do I need to manage managed resources?

I have been looking at the standard Dispose pattern and I'm just wondering what I need to write to free managed resources? If these resources are 'managed' already then surely I shouldn't need to do anything. If that's the case, and my class doesn't hold any unmanaged resources (hence no need for it to be finalized by GC) then do I only...

Can't release Excel COM object in .NET

Hello, I have created a tool that imports an excel sheet. The excel COM object is created during the lifetime of the app. I have applied the MVP pattern to my tool so that VIEW and Presenter are seperating the UI and logic. The vIEW that is a WinForm is having a Dispose() method due inheritance from From class, which is overriden in th...

SPWeb disposing himself and causing exception

Hello. When trying to use SPWeb.GetSiteData(SPSiteDataQuery) (Trying to bind some data to SPGridView), it throws me an TargetInvocationException with a following stacktrace: Exception at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTyp...

Properly disposing of class with thread

I have a fairly complex multi-threaded Windows service working, but I can't figure out how to clean up correctly. Below is some [pseudo] code to show what I have. The actual code is much more complex, probably too much to copy/paste here. Basically, I have a class Request that creates a thread to do the work. When a new request comes...

Is mutex correctly implemented and how do I dispose it?

I am reviewing some code and one of the code analysis (fxCop) warnings has gotten me very confused. The code implements a few mutex's by creating variables at the start of the class, similar to this: private Mutex myMutex = new Mutex(); fxCop is popping up with a message saying that I must implement IDisposable for the class as the Mu...

Is re-using GDI+ objects bad practice? (or: How to use many nested using blocks without getting headaches?)

I'm currently writing a fairly complex paint method for a user control, involving a fair amount of drawing code. I know that all GDI+ resources need to be properly disposed so I wrap each of those in a using block. But when I noticed that I used three using blocks for three different SolidBrushes I wondered whether I couldn't just re-us...

C# Not Disposing controls like I told it to...

I have a Panel control. And inside the panel users can add combobox's, textbox's labels etc and drag them around and stuff, and there's a Delete button on my form where if they click it, it will delete all controls inside that panel. BUT this code: foreach( Control control in panel.Controls ) { control.Dispose(); } ... Does not w...

How do I dispose all of the controls in a panel or form at ONCE??? c#

is there a way to do this? ...

Handling with temporary file stream

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

Clearing up large fields from memory in long lived objects

.NET 3.5, I've got some classes which stores up to 1MB of strings. Even though I need the object for a really long time I don't need to store the string for a long time. How can I truly remove the string from memory without disposing the parent object. Is it a good practice to use "myString = null" in this case? or shall wrap it in a ...

How to dispose a Writeable Bitmap? (WPF)

Some time ago i posted a question related to a WriteableBitmap memory leak, and though I received wonderful tips related to the problem, I still think there is a serious bug / (mistake made by me) / (Confusion) / (some other thing) here. So, here is my problem again: Suppose we have a WPF application with an Image and a button. The ima...

Strange GDI+ behaviour.

I have made a method to CompressImageSize according to Image quality. The code for it is public static Image CompressImage(string imagePath, long quality) { Image srcImg = LoadImage(imagePath); //Image srcImg = Image.FromFile(imagePath); EncoderParameters parameters = new EncoderParameters(1); parameters.Param[0] = new ...

Proper cleanup of WPF user controls

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

Does a wrapper class calling a COM component through C# need to implement the Dispose pattern?

I have a class written in c# which is acting as a wrapper around a COM component. The COM component is early bound and the RCW has been generated by Visual Studio. Should I implement a dispose pattern in my wrapper class to clean up the COM reference, or should I just let the GC handle it, as it already has a RCW? ...

Do I need to call Graphics.Dispose()?

In a VB.NET program I'm creating a new bitmap image, I then call Graphics.FromImage to get a Graphics object to draw on the bitmap. The image is then displayed to the user. All the code samples I've seen always call .Dispose() on Bitmaps and Graphics objects, but is there any need to do that when neither have touched files on disk? Are ...

Event Sender Gets Disposed In Client's Event Handler Code

Hello, I'm facing the following situation (C#/.Net here, but I think it's a general problem): Some of our object types (which are collections) are disposable types (IDisposable in C#, which allows clients to explicitly tell an object 'you are not needed anymore, free all of your resources') These collections fire events ('oh my, look,...

Purpose of Dispose calling Dispose(IsDisposing) pattern in C#?

Here is code from MSDN. I don't understand why the work isn't just done in the regular Dispose() method here. What is the purpose of having the Dispose(bool) method? Who would ever call Dispose(false) here? public void Dispose() { Dispose(true); // Use SupressFinalize in case a subclass // of this type implements a fina...

Am I responsible for Disposing a BackgroundImage?

Hi, I have a windows form where I set the BackgroundImage property to a custom bitmap image. private Image MakeCustomBackground() { Bitmap result = new Bitmap(100, 100); using(Graphics canvas = Graphics.FromImage(result)) { // draw the custom image } return result; } private void UpdateFromBackground() { ...