Gdi objects and memory leak


D

Duff

Hi all,
I am facing some serious issues with an application written in c#. I noticed
(using Task Manager) that the number of GDI objects is increasing during the
use of the application which crashes. I don't know how to locate the problem
in the application and have several questions:
- what does represent the number of GDI objects shown in the task manager?
- how can I locate the unused gdi objects and destroy them?
- can this be related to the fact that classes belonging to System.Drawing
causes memory leaks?

Please help, I have to solve that issue and it makes me crazy!!!!

Thx
 
Ad

Advertisements

G

Guest

Are you sure you are looking at a memory leak? Are you consequently disposing
the brushes, images and what more using either the Dispose method or the
using statement?

Are you using GDI objects yourself? In that case, are you implementing the
IDisposable interface and releasing the GDI handles appropriately?
 
R

Richard Grimes

Duff said:
Hi all,
I am facing some serious issues with an application written in c#. I
noticed (using Task Manager) that the number of GDI objects is
increasing during the use of the application which crashes. I don't
know how to locate the problem in the application and have several
questions: - what does represent the number of GDI objects shown in
the task
manager?
They are handles to things like unmanaged pens and brushes.
- how can I locate the unused gdi objects and destroy them?
Whenever you create a System.Drawing.Pen or Brush make sure that you
call Dispose on the object after you have finished using it. *However*,
if you use a Pen from Pens or a Brush from Brushes *do not* dispose it.
The reason is that Pens and Brushes contain static members which are
created the first time they are accessed, if you Dispose such a member,
the actual object will be disposed, but there will be an invalid
reference in the static member, and hence when you access it you'll get
an object disposed exception. This might be a cause of your 'leak' if
you use lots of different brushes or pens through Brushes and Pens.
- can this be related to the fact that classes belonging to
System.Drawing causes memory leaks?
I don't think so. Check all the code that creates Pen and Brush objects
and dispose them correctly. Pay particular attention to loops:

for (int i = 0; i < 100; i++)
{
Pen p = new Pen(Color.Black);
g.DrawLine(p, x1, y1, x2, y2);
}

Here, 100 pens are created and are not disposed. If you have code like
this take the declaration of the pen out of the loop and dispose it
after the loop:

Pen p = new Pen(Color.Black);
for (int i = 0; i < 100; i++)
{
g.DrawLine(p, x1, y1, x2, y2);
}
p.Dispose();

Richard
 
D

Duff

Okay...
Although I'm convinced my code is 'clean' (IDisposable implementation,
etc...) I will take another look at that.
But I tried to locate the problem using a profiler (.Net Memory Profiler
v.2.6) and noticed that after 50 minutes of use I got about 10 000
'undisposed' Bitmap class instances. That's why I was expected
System.Drawing to cause memory leaks....

To be continued...

Thanks
 
D

Duff

Thanks for your usefull help.
I located the issue which actually was a lack of GDI objects disposing.

Thanks again.
Duff
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

GDI leaks? 1
Memory Leak with Unmanaged Objects 0
Is this a memory leak? 5
Problem: too many GDI objects in memory 1
GDI object usage 2
bchar* memory leak 0
Memory leak problem 1
Memory Leak with Service? 3

Top