Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dispose vs Dispose(bool)

Tags:

c#

dispose

I am confused about dispose. I am trying to get my code disposing resources correctly. So I have been setting up my classes as IDisposable (with a Dispose method) them making sure that the Dispose method gets called.

But now FXCop is telling me lots of stuff about disposing = false and calling Dispose(false).

I don't see a Dispose method that takes a bool. Do I need to make one? If so, why? Why not just have a method that gets called when it is disposing?

I saw some code here: CA1063: Implement IDisposable correctly - Microsoft Docs that shows how to make a Dispose method that takes a bool. It says it is for native vs managed resourses. But I thought the whole point of dispose was for unmanaged resourses only.

Also, the line that FXCop is complaining about is this:

~OwnerDrawnPanel() {     _font.Dispose(); } 

It says:

CA1063 : Microsoft.Design : Modify 'OwnerDrawnPanel.~OwnerDrawnPanel()' so that it calls Dispose(false) and then returns.

But Font does not have a Dispose(bool) on it (that I can find).

To sum it up:

Why do I need a Dispose(bool)? and if I do, why doesn't Font have it? and since it does not have it, why is FXCop asking me to use it?


Thanks for all the great answers. I think I understand now. Here is

The answer as I see it:

Disposing of "unmanaged" resources falls into two categories:

  1. Resources that are wrapped in a managed class (ie Bitmap, Font etc), but still need Dispose to be called to clean them up properly.
  2. Resources that you have allocated, which are representations of native resources (ie device contexts that need to be released)

Dispose(bool) is used to tell the difference between the two:

  1. When Dispose is directly called on your object, you want to free both kinds of "unmanaged" resources.
  2. When your object is up for Garbage Collection, you don't need to worry about the first kind of resources. The garbage collector will take care of them when it cleans them up. You only need to worry about true native resources that you have allocated (if any).
like image 833
Vaccano Avatar asked Mar 07 '11 20:03

Vaccano


People also ask

What is Dispose () in C#?

In the context of C#, dispose is an object method invoked to execute code required for memory cleanup and release and reset unmanaged resources, such as file handles and database connections.

Is Dispose called automatically C#?

Dispose() will not be called automatically. If there is a finalizer it will be called automatically. Implementing IDisposable provides a way for users of your class to release resources early, instead of waiting for the garbage collector.

When should you call Dispose?

There is a simple guideline: if you have finished with an object whose type implements IDisposable then call Dispose() on it; you should use a using block to do this.

Does destructor call Dispose C#?

Destructors Versus DisposeIt is not legal to call a destructor explicitly. Your destructor will be called by the garbage collector. If you do handle precious unmanaged resources (such as file handles) that you want to close and dispose of as quickly as possible, you ought to implement the IDisposable interface.


1 Answers

Dispose(bool) is a pattern to implement Finalize and Dispose to Clean Up Unmanaged Resources , see this for detail

like image 98
Kumar Avatar answered Oct 04 '22 08:10

Kumar