Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 graphic is not visible, such as on a GUI that has tabs and the user switched to the other tab, and then redraw it when they switch back?
  • Will I break things if I call it when I shouldn't?
  • Will Batman escape the evil clutches of the Joker?
like image 449
lala Avatar asked Jul 03 '10 10:07

lala


People also ask

What does Graphics Dispose do?

Calling Dispose allows the resources used by this Graphics to be reallocated for other purposes.

Why do we have the Dispose () method?

The dispose pattern is used for objects that implement the IDisposable interface, and is common when interacting with file and pipe handles, registry handles, wait handles, or pointers to blocks of unmanaged memory. This is because the garbage collector is unable to reclaim unmanaged objects.

Do I need to call Dispose C#?

Rule of thumb: if a class implements IDisposable you should always call the Dispose method as soon as you have finished using this resource. Even better wrap it in a using statement to ensure that the Dispose method will be called even if an exception is thrown: using (var reader = conn.


2 Answers

  • When should I be using dispose() on a code-drawn graphic? Whenever you are completely done with any object that implements IDisposable, you should call Dispose just before it becomes eligible for garbage collection. As others have pointed out, it's best to use the using statement instead of calling Dispose directly.
  • What happens if I don't? Your program will be slightly less efficient because it will take up slightly more resources than necessary. This is the only drawback to not disposing graphics; however, not disposing other classes can actually cause errors (one famous example is StreamWriter). For this reason, it's best to always dispose any class that implements IDisposable, as a general rule.
  • Do I need to call it every time a graphic is not visible, such as on a GUI that has tabs and the user switched to the other tab, and then redraw it when they switch back? No. Objects should only be disposed when you are completely done with them.
  • Will I break things if I call it when I shouldn't? Yes. If you call Dispose before you are done with the object, and then attempt to use the disposed object, you will get an ObjectDisposedException.
  • Will Batman escape the evil clutches of the Joker? Tune in tomorrow, same bat-time, same bat-channel!
like image 193
Stephen Cleary Avatar answered Oct 10 '22 06:10

Stephen Cleary


When you ask for a graphical object, Windows will allocate a bit of memory for you. Calling dispose will tidy up that memory for you. If you do not call dispose, all of these handles to memory wil remain open and eventually your system will run out of resources, become slower and eventually stop (closing the program may free them however).

Because you're using .NET, when you have finished using your graphics object, the garbage collector will eventually call dispose for you. The problem with the garbage collector is you never know when it will clean up the object, so it may leave these resources open for longer than necessary.

This said, you should never have to call dispose yourself. Far better will be to put your object in using scope:

using(Graphics g)
{
    // do something with the resource
}

Now when you leave this using scope, the object will be destroyed and dispose will be called automatically for you. You should put all objects that have the dispose method defined inside a using scope.

like image 25
DanDan Avatar answered Oct 10 '22 06:10

DanDan