PC Review


Reply
Thread Tools Rate Thread

Gdi objects and memory leak

 
 
Duff
Guest
Posts: n/a
 
      3rd Dec 2005
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


 
Reply With Quote
 
 
 
 
=?Utf-8?B?SmVsbGUgdmFuIGRlciBCZWVr?=
Guest
Posts: n/a
 
      3rd Dec 2005
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?

"Duff" wrote:

> 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
>
>
>

 
Reply With Quote
 
 
 
 
Richard Grimes
Guest
Posts: n/a
 
      3rd Dec 2005
Duff wrote:
> 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[i], y1[i], x2[i], y2[i]);
}

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[i], y1[i], x2[i], y2[i]);
}
p.Dispose();

Richard
--
http://www.grimes.demon.co.uk/workshops/fusionWS.htm
http://www.grimes.demon.co.uk/workshops/securityWS.htm


 
Reply With Quote
 
Duff
Guest
Posts: n/a
 
      3rd Dec 2005
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


 
Reply With Quote
 
=?Utf-8?B?SmVsbGUgdmFuIGRlciBCZWVr?=
Guest
Posts: n/a
 
      3rd Dec 2005
A great resource on this particular matter is 'Designing for a memory managed
world':

http://msdn.microsoft.com/netframewo...managedmemory/


"Duff" wrote:

> 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
>
>
>

 
Reply With Quote
 
Duff
Guest
Posts: n/a
 
      5th Dec 2005
Thanks for your usefull help.
I located the issue which actually was a lack of GDI objects disposing.

Thanks again.
Duff


 
Reply With Quote
 
=?Utf-8?B?SmVsbGUgdmFuIGRlciBCZWVr?=
Guest
Posts: n/a
 
      6th Dec 2005
Hey, that's great! Thanks for letting us know!

"Duff" wrote:

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

 
Reply With Quote
 
New Member
Join Date: May 2012
Posts: 1
 
      8th May 2012
I usually use deleaker or similar debugger for GDI leaks in such cases...

Quote:
Originally Posted by =?Utf-8?B?SmVsbGUgdmFuIGRlciBCZWVr?= View Post
Hey, that's great! Thanks for letting us know!

"Duff" wrote:

> Thanks for your usefull help.
> I located the issue which actually was a lack of GDI objects disposing.
>
> Thanks again.
> Duff
>
>
>
Your nick - is XSS?
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make MODI.Document not leak memory? I have millions of images to OCR but each time i OCR one it leaks memory. How to do the OCR and then clean up so that my app doesnt leak memory? DR Microsoft C# .NET 2 5th Feb 2008 03:52 AM
How to make MODI.Document not leak memory? I have millions of images to OCR but each time i OCR one it leaks memory. How to do the OCR and then clean up so that my app doesnt leak memory? DR Microsoft Dot NET 1 5th Feb 2008 12:43 AM
How to make MODI.Document not leak memory? I have millions of images to OCR but each time i OCR one it leaks memory. How to do the OCR and then clean up so that my app doesnt leak memory? DR Microsoft Dot NET Framework 1 5th Feb 2008 12:41 AM
GDI+ and GDI interop mix failing on Graphics objects oskilian ~at~ rapapaing #dot# com Microsoft Dot NET Framework Forms 1 10th Oct 2006 10:18 AM
over 4297000000 of GDI objects in a Windows.Forms application! ListView's leak. Wiktor Zychla Microsoft Dot NET Framework Forms 13 18th Sep 2003 10:45 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:22 PM.