Page 1 of 1

Possible cause for crashes missing dispose for graphic objec

Posted: Mon Jul 06, 2015 9:05 am
by mawa
Looking at the code I found several code locations in Camera.cs where memory leaks may result.

Almost all graphic objects are references and contain references to unmanaged code. Therefore when you do not explicitly dispose a graphic object the garbage collector may handle the managed part but omits the unmanaged part resulting in a memory leak.

Here's an example for the safest way to handle graphic objects (found @ http://stackoverflow.com/questions/1209 ... -necessary :

Code: Select all

using (Bitmap bmp = new Bitmap(100, 100))
{
  using (Graphics g = Graphics.FromImage(bmp))
  {
    using (Pen p = new Pen(Color.FromArgb(128, Color.Blue), 1))
    {
      using (Brush b = new SolidBrush(Color.FromArgb(128, Color.Blue)))
      {
        g.FillEllipse(b, 0, 0, 99, 99);
        g.FillRegion(b, pictureBox1.Region);

        pictureBox1.BackColor = Color.Transparent;
        pictureBox1.Image = bmp;
      }
    }
  }
or

Code: Select all

using (Bitmap bmp = new Bitmap ( 100, 100 ))
using (Graphics g = Graphics.FromImage ( bmp ))
using (Pen p = new Pen ( Color.FromArgb ( 128, Color.Blue ), 1 ))
using (Brush b = new SolidBrush ( Color.FromArgb ( 128, Color.Blue ) ))
{
    g.FillEllipse ( b, 0, 0, 99, 99 );    
    g.FillRegion ( b, pictureBox1.Region );
}
then the objects are removed in all cases, even in case of an exception.

Due to th heavy use of the Draw... methodes the memory loss may sum up.

Re: Possible cause for crashes missing dispose for graphic o

Posted: Mon Jul 06, 2015 6:33 pm
by thereza
yeah it leaks really bad but the garbage collection does kick in and clean it up every so often. i ran the code through some program that looked at memory and it seems like garbage collection keeps it from going out of control.

Re: Possible cause for crashes missing dispose for graphic o

Posted: Mon Jul 06, 2015 8:25 pm
by mawa
I do heavy graphic drawing and image display in my companies CRM program and recommend the using statement because the GC does not handle the unmanaged windows draw api objects. It sure takes quite a while before you suddenly don't see the expected pen or brush color.

BTW try/catch won't help here very much because - when graphics memory is gone it's gone :evil:

Re: Possible cause for crashes missing dispose for graphic o

Posted: Mon Jul 06, 2015 8:40 pm
by thereza
mawa wrote:I do heavy graphic drawing and image display in my companies CRM program and recommend the using statement because the GC does not handle the unmanaged windows draw api objects. It sure takes quite a while before you suddenly don't see the expected pen or brush color.

BTW try/catch won't help here very much because - when graphics memory is gone it's gone :evil:
yeah - your approach is ideal - but just saying that after looking at a memory pro filer, the code is working ok as is. there are a ton of things that are done inefficiently and leak memory - graphics being the biggest culprit, but garbage collection seems to work OK on it.

Re: Possible cause for crashes missing dispose for graphic o

Posted: Tue Jul 07, 2015 4:07 pm
by mawa
Please take look here: https://msdn.microsoft.com/en-us/librar ... 90%29.aspx
Robust Programming

You should always call Dispose on any objects that consume system resources, such as Brush and Graphics objects.
It is MANDATORY to dispose() graphic objects. If not you will eat up GDI resources. Windows has quotas for the number of User and GDI objects that a process can use at a given time. The default values are 10,000 for GDI objects. And please note GDI Objects are not handled by the GC. If you call Dispose() on the managed "wrapper" of a GDI object the objects is cleanly returned to the available GDI resources.

And BTW Those little using statements don't hurt. :D

Here more on memory leaks: http://madgeek.com/articles/leaks/leaks.en.html