#GC Handles leak

Topics related to the Krypton Toolkit.

Moderators: Phil Wright, Chris Porter

#GC Handles leak

Postby tm05902 » Mon Aug 23, 2010 9:36 pm

Hello,

I've been experiencing some exceptions with the application I'm developing.
The exception is Win32Exception, Error creating window handle.

I programatically create lots of user controls & remove them, the problem seems to be that the handles creaded when the controls are added to the container are not correctly released when the controls are removed.

I've noticed that User controls that contain krypton controls don't receive the call to Dispose (or destructor) method

I've been able to reproduce the problem with this code:

I monitor the GC Handles increase with Process Esplorer (.Net/.Net CLR Memory/# GC Handles). While I'm clicking the button the handles don't stop increasing until reach the machine limit (in my case about 37000) and a exception is thrown.

Using Button instead of KryptonButton works fine, handles remain constant.

Code: Select all
        private void button1_Click(object sender, EventArgs e)
        {

            flowLayoutPanel1.Controls.Clear();
            GC.Collect();

            List<KryptonButton> testList = new List<KryptonButton>();
            for (int i = 0; i < 200; i++)
            {
                KryptonButton b = new KryptonButton();
                testList.Add(b);
            }

            flowLayoutPanel1.Controls.AddRange(testList.ToArray());
        }
tm05902
 
Posts: 1
Joined: Mon Aug 23, 2010 9:09 pm

Re: #GC Handles leak

Postby shartmann » Wed Oct 20, 2010 12:09 pm

same here with KryptonLabels (current version 4.3.0)
I sent Phil a PM
I hope this get fixed asap
shartmann
 
Posts: 45
Joined: Thu Mar 27, 2008 5:58 pm

Re: #GC Handles leak

Postby shartmann » Mon Nov 08, 2010 4:30 am

anything new ? :(
shartmann
 
Posts: 45
Joined: Thu Mar 27, 2008 5:58 pm

Re: #GC Handles leak

Postby Phil Wright » Tue Nov 09, 2010 2:26 pm

Looking at the sample I can see how to resolve the issue. You just need to call Dispose on the KryptonLabel when you remove it from the parent collection.

The KryptonLabel, and this is true or all Krypton controls, will not automatically dispose of itself when you think there are no references to it. This is because each Krypton control maintains a set of references to the global KryptonManager. This allows the control to know when the palette has changed and so update its appearance. This means that although you think there are no references to this control in your own code, in practice there are some hidden references occurring that prevent it being disposed automatically. Hence you have to explicitly call the Dispose method. This is the way Krypton has worked since the very beginning and so the behaviour has not recently changed.

The standard windows Label control does not have any internal references that would keep it alive and so their control will automatically dispose itself once your application no longer has any references to it.
Phil Wright
Site Admin
 
Posts: 2720
Joined: Thu Apr 13, 2006 2:55 pm
Location: Melbourne, Australia

Re: #GC Handles leak

Postby shartmann » Tue Nov 09, 2010 3:26 pm

oooh, thats sounds really bad to me :shock:
I thought about the references, but I thought they were kept by error and not by design :(
In my eyes this totaly breaks programming style of C# / .Net where you regularly don't need to take care of Dispose.
shartmann
 
Posts: 45
Joined: Thu Mar 27, 2008 5:58 pm

Re: #GC Handles leak

Postby Phil Wright » Tue Nov 09, 2010 3:35 pm

The automatic calling of Dispose is nice but as a matter of course you should be calling dispose anyway when you know it is needed. Most of the time this is really not an issue. When you Dispose a Form it automatically calls dispose on all children and so on down the chain. It is only a problem when you manually remove controls from a collection and most programs don't actually do that. And if they do then you just need to call dispose yourself.
Phil Wright
Site Admin
 
Posts: 2720
Joined: Thu Apr 13, 2006 2:55 pm
Location: Melbourne, Australia

Re: #GC Handles leak

Postby colonel32 » Tue Nov 09, 2010 11:10 pm

Phil Wright wrote:It is only a problem when you manually remove controls from a collection and most programs don't actually do that.

Dispose is only called automatically after a Form.Show(). If you call Form.ShowDialog() you must dispose yourself afterwards. Particularly important with Krypton.
Last edited by colonel32 on Wed Nov 10, 2010 12:37 am, edited 2 times in total.
colonel32
 
Posts: 34
Joined: Tue Oct 13, 2009 1:31 am

Re: #GC Handles leak

Postby shartmann » Tue Nov 09, 2010 11:44 pm

colonel32 wrote:Dispose is only called automatically after a Form.Show(). If you call Form.ShowDialog() you must dispose yourself afterwards. Particularly important with Krypton.

This case is a little bit different:
On top I have panel, where some controls are placed dynamicly.
To remove them I just call Panel.Controls.Clear();
If I use regular controls like button, label, textbox ... they are all free'd after the Clear.
If I use KryptonButton, KryptonLabel, KryptonTextbox ... they are not free'd, I have to dispose every single one them.
This is not the way I would expect components to work in .Net.

In my eyes this is fault in the design of the Krypton Controls,
but I understand that this might not be easy to fix without breaking half of Krypton code ...
shartmann
 
Posts: 45
Joined: Thu Mar 27, 2008 5:58 pm

Re: #GC Handles leak

Postby colonel32 » Wed Nov 10, 2010 12:39 am

shartmann wrote:This case is a little bit different:


Sorry, I could've been clearer. I was reponding to Phil's post just above mine, not trying to argue about your use case.
colonel32
 
Posts: 34
Joined: Tue Oct 13, 2009 1:31 am

Re: #GC Handles leak

Postby Consulteware » Tue Mar 01, 2011 11:54 pm

Hi Phil, any news regarding this bug?
I have this problem until now from version 3, I need to solve this but I cannot do it because appears on any screen in any time of the working of the application.

Regards
Consulteware
 
Posts: 8
Joined: Tue Aug 18, 2009 6:02 am

Re: #GC Handles leak

Postby colonel32 » Tue Mar 01, 2011 11:58 pm

This isn't a Krypton bug, winforms Panel.Controls.Clear() does not call Dispose() on each contained control either. Google it and you will find examples of recursive Dispose() code you can call manually.

I researched this myself last week, after having exactly the same problem with a System.Windows.Forms.TableLayoutPanel - nothing to do with Krypton.
colonel32
 
Posts: 34
Joined: Tue Oct 13, 2009 1:31 am

Re: #GC Handles leak

Postby Consulteware » Thu Apr 14, 2011 1:22 am

I have searched the web, and find some articles but none with this specific problem, can you tell me where you find that information?

Regards
Consulteware
 
Posts: 8
Joined: Tue Aug 18, 2009 6:02 am


Return to Krypton Toolkit

Who is online

Users browsing this forum: No registered users and 1 guest

cron