Questions tagged [dispose]

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources

Microsoft.NET

The pattern for disposing an object, referred to as a dispose pattern, imposes order on the lifetime of an object. The dispose pattern is used only for objects that access unmanaged resources. This is because the garbage collector is very efficient at reclaiming unused managed objects.

A type's Dispose method should release all the resources that it owns. It should also release all resources owned by its base types by calling its parent type's Dispose method. The parent type's Dispose method should release all resources that it owns and in turn call its parent type's Dispose method, propagating this pattern through the hierarchy of base types.

Reference

1987 questions
29
votes
3 answers

Determining if IDisposable should extend an interface or be implemented on a class implementing said interface

How can I determine if I should extend one of my interfaces with IDisposable or implement IDisposable on a class that implements my interface? I have an interface that does not need to dispose of any external resources, except for one particular…
Peter Rilling
  • 323
  • 3
  • 7
27
votes
9 answers

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…
Auron
  • 13,626
  • 15
  • 47
  • 54
26
votes
3 answers

C# USING keyword - when and when not to use it?

I'd like to know when i should and shouldn't be wrapping things in a USING block. From what I understand, the compiler translates it into a try/finally, where the finally calls Dispose() on the object. I always use a USING around database…
Andrew Bullock
  • 36,616
  • 34
  • 155
  • 231
25
votes
7 answers

How to handle exception thrown from Dispose?

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 { …
Morgan Cheng
  • 73,950
  • 66
  • 171
  • 230
24
votes
3 answers

Right way to dispose Image/Bitmap and PictureBox

I am trying to develop a Windows Mobile 6 (in WF/C#) application. There is only one form and on the form there is only a PictureBox object. On it I draw all desired controls or whatever I want. There are two things I am doing. Drawing custom shapes…
Kornelije Petak
  • 9,412
  • 15
  • 68
  • 96
23
votes
4 answers

Does calling Clear disposes the items also?

Many times there is a clear method, that removes all the items from the collections, are these items disposed also. Like, toolStripMenuItem.DropDownItems.Clear(); is sufficient, or should I have to call like that: foreach (ToolStripItem item in…
Priyank Bolia
  • 14,077
  • 14
  • 61
  • 82
23
votes
5 answers

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…
jrista
  • 32,447
  • 15
  • 90
  • 130
23
votes
1 answer

Is it necessary to close the Stream of WebInvoke method

I have a service interface with a method that has a parameter of type Stream. Should i close the stream after i have read all data from this stream or is this done by the WCF Runtime when the method call is completed? The most examples i have seen,…
Jehof
  • 34,674
  • 10
  • 123
  • 155
22
votes
6 answers

What is IDisposable for?

If .NET has garbage collection then why do you have to explicitly call IDisposable?
FendFend
  • 289
  • 1
  • 3
  • 6
22
votes
3 answers

Ninject and DataContext disposal

I'm using Ninject to retrieve my DataContext from the kernel and I was wondering if Ninject automatically disposes the DataContext, or how he handles the dispose() behaviour. From own experiences I know disposing the datacontext is pretty important…
Bas
  • 1,232
  • 1
  • 12
  • 26
22
votes
4 answers

How to wait for a single event in C#, with timeout and cancellation

So my requirement is to have my function wait for the first instance an event Action coming from another class and another thread, and handle it on my thread, allowing the wait to be interrupted by either timeout or CancellationToken. I want to…
lobsterism
  • 3,469
  • 2
  • 22
  • 36
20
votes
3 answers

Dispose/Close ExchangeService in C#?

I'm using the ExchangeService WebService API (Microsoft.Exchange.WebServices.Data) but I cannot find any Close or Dispose method. Is it not neccessary to close the connection somehow? My method looks like this: public void CheckMails() { …
Simon Woker
  • 4,994
  • 1
  • 27
  • 41
20
votes
5 answers

How do I prevent a form object from disposing on close?

I am using an MDIParent Form. When I close its child, the object of the child disposes. Is there a way to set child visibility to false instead of disposing?
Touseef Khan
  • 413
  • 5
  • 11
  • 26
20
votes
5 answers

Should Dispose methods be unit tested?

I am using C#. Is it advised to unit test dispose methods? If so why, and how should one test these methods?
sbenderli
  • 3,654
  • 9
  • 35
  • 48
20
votes
4 answers

When do I need to use dispose() on graphics?

I'm learning to draw stuff in C# and I keep seeing recommendations to use dispose(), but I don't quite understand what it does. When should I be using dispose() on a code-drawn graphic? What happens if I don't? Do I need to call it every time a…
lala
  • 2,052
  • 6
  • 24
  • 33