Quantcast
Viewing all articles
Browse latest Browse all 12583

Font causes GDI leak in custom control

I have created a custom control like below.

publicpartialclassTextBoxEx:TextBox{publicTextBoxEx(){InitializeComponent();Font=Utility.normalFont;}protectedoverridevoidOnPaint(PaintEventArgs pe){base.OnPaint(pe);}}//A utility class to initialize font.   classUtility{internalstaticFont normalFont =newFont("Arial",18);}

I have two forms Form1 and Form2. This TextBoxEx is added to Form2. I am showing Form2 when clicking a button in Form1.

When I continuously open and close Form2, GDI count in TaskManager keeps increasing by 1. After analysing with a GDI detection tool (Bear.exe), it is found that the Font causes GDI leak. After increasing the GDI count to some count (say 60 or 70), it will automatically reset back to default count (say 4 or 8).

But when I set Font=null in Dispose() method of TextBoxEx, GDI leak disappears. Is it recommended to set Font as NULL in Dispose() method of TextBox control?.

Should we really need to release the Font handle or does this increase is expected ?



Viewing all articles
Browse latest Browse all 12583

Trending Articles