Windows Develop Bookmark and Share   
 index > Windows Forms General > GDI handling (leak). How find best solution for that ?
 

GDI handling (leak). How find best solution for that ?

Hi people,

I'd like to know how can handl GDI in .NET (C#), probably you could explain me more detailed basic aspects or/and show an example or/and provide me with helpful links.

Thank you in advance !
  • Moved byDavid M MortonMVP, ModeratorThursday, September 03, 2009 1:16 PMGDI... More Windows Forms than C# General (From:Visual C# General)
  •  
Julian Ustiyanovcyh  Thursday, September 03, 2009 8:22 AM
It is very difficult to leak GDI+ objects, they all have a finalizer. The tool you need is built into Windows: Start + Run, Taskmgr.exe, Processes tab. View + Select columns, check "GDI Objects". You'd better add USER32 Objects too, that's the more typical leak.

Hans Passant.
nobugz  Friday, September 04, 2009 4:46 PM
.net actually uses GDI+, which is kind of like GDI (though I am not sure how as I have never used, personally, GDI).

Here is an example of GDI+:

public class MyControl : System.Windows.Forms.Control
{
public MyControl()
{
}

public override OnPaint(object sender, System.Windows.Forms.PaintEventArgs pe)
{
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(10,10,10,10);
pe.Graphics.DrawRectangle(Pens.Black, rect);
}
}

The above example will draw a rectangle on your control. I believe that all of the GDI+ methods are contained in the System.Drawing.Graphics class. This class is sealed, so it can't be inherited from. Also, the class, on it's own does nothing. You can override the OnPaint event of any control and use the PaintEventArgs.Graphics, or you can use the System.Windows.Forms.Control.CreateGraphics() method.

I hope thid helps.
There are 10 types of people in this world, those who understand Binary, and those who don't.
anubisascends  Thursday, September 03, 2009 12:08 PM
Well, probably I have ask in not very correct form. Actually I need to find approach how to check for GDI leaks.

Scenario:

Run an application, and check if all GDI objects are disposed when I go to close the window. Because I think that probably not all GDI object are disposed. I need to have kind of app. that could monitoring this process.


Thank you
Julian Ustiyanovcyh  Thursday, September 03, 2009 12:58 PM
Hi Julian,

This article discusses about how to dispose GDI+ resources: http://www.codeproject.com/KB/GDI-plus/DisposeGDI.aspx?display=Print.
This article is about .Net memory leak: http://blogs.msdn.com/tess/archive/2009/02/03/net-memory-leak-to-dispose-or-not-to-dispose-that-s-the-1-gb-question.aspx.

Let me know if this helps or not.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, September 04, 2009 10:13 AM
It is very difficult to leak GDI+ objects, they all have a finalizer. The tool you need is built into Windows: Start + Run, Taskmgr.exe, Processes tab. View + Select columns, check "GDI Objects". You'd better add USER32 Objects too, that's the more typical leak.

Hans Passant.
nobugz  Friday, September 04, 2009 4:46 PM
Is there some add-ins for VS2008 (C#) that could detect GDI leaks and show where exactly in the source code each GDI object was created ?
Julian Ustiyanovcyh  Wednesday, September 16, 2009 12:39 PM
A profiler, maybe. They tend to track managed objects, not unmanaged ones like a GDI handle. Shop around and read the small print.

Hans Passant.
nobugz  Wednesday, September 16, 2009 12:51 PM

You can use google to search for other answers

Custom Search

More Threads

• Complex user control not responding to click events
• Populating Textbox from Combo Box Selection
• monitor folder
• listView and form
• openFileDialog trouble keeping the path
• XML and XSLT in WinForm
• Office 2003
• Default Value for DateTimePicker
• image overlay fade help...
• ContextMenuStrip check boxes to work like RadioButtons?