Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET GDI custom-controls , +
I have created a custom control like below.
 
public partial class TextBoxEx : TextBox
{
    public TextBoxEx()
    {
        InitializeComponent();
        Font = Utility.normalFont;
    }
 
    protected override void OnPaint(PaintEventArgs pe)
    {
        base.OnPaint(pe);
    }
}
    //A utility class to initialize font.   
    class Utility
    {
        
        internal static Font normalFont = new Font("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.
 
Continuously showing and closing Form2 causes GDI leak in my application. After analysing with a GDI detection tool(Bear.exe), it is found that the Font causes GDI leak.
 
My question is,
1. Why the Font is not released even though Dispose() method of TextBoxEx is get called.(While closing Form2, Dispose() method of TextBoxEx will be invoked automatically).
2. How can I solve GDI leak caused by Font?.
(Font.Dispose() cannot be called in Dispose() method of TextBoxEx. Because it throws "Parameter is not valid" exception in the constructor).
Posted 28-Apr-13 1:10am
Comments
Sergey Alexandrovich Kryukov at 28-Apr-13 13:46pm
   
1) How do you know that something is not disposed? How, exactly?
2) Code sample, please.
—SA
Kaizen202 at 2-May-13 4:54am
   
Code sample is already posted in the Question. Please check above code snippet.
By checking GDI objects in TaskManager, it is found that GDI objects in my Application keeps increasing. After analysing more with Bear.exe (Tool to detect GDI objects[http://thesz.diecru.eu/content/bear.php]),I just confirmed that Font causes the increase in GDI objects.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 28 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100