ryptoWinforms库是否有内存泄漏问题

问题描述

|| 我目前正在调试一个大型Winforms应用程序,该应用程序存在一些内存泄漏问题。我使用.NET内存分析器,到目前为止,我已经能够找到其中的一些泄漏并加以解决。但是现在我面临着一个不确定的问题,如果这个问题我不知道如何解决。 在运行我的应用程序大约1分钟后(考虑到普通用户可以使用它几个小时,这并不算多),. NET内存探查器向我显示了大约100-200个来自Krypton Toolkit的不同控件的实例,并且如果我不这样做,这个数字将会增加继续前进(它们永远不会被垃圾回收,因为看起来它们仍然在某个地方被引用)。现在,如果我检查这些实例的根路径,它们看起来都像: 我不知道在哪里查找我的代码以使这些实例在不再需要时被正确取消引用,因为我不知道仍在引用该控件。我知道创建KryptonButtonEx的位置,并且据我所知,ViewManager是通过此按钮创建的,但是我仍然看不到该怎么做。对于那些感兴趣的人,创建按钮的代码是这样的:
        KryptonButton newControlButton = new KryptonButton();
        newControlButton.Tag = mtActivityControl;
        newControlButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
        newControlButton.AutoSize = true;
        newControlButton.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly;
        newControlButton.ButtonStyle = ComponentFactory.Krypton.Toolkit.ButtonStyle.ListItem;
        newControlButton.Location = new System.Drawing.Point(3,3);
        newControlButton.Name = string.Format(\"controlButton{0}\",mtActivityControl.SymbolicName);
        newControlButton.Size = new System.Drawing.Size(96,23);
        newControlButton.StateCommon.Content.Image.ImageH = ComponentFactory.Krypton.Toolkit.PaletteRelativeAlign.Near;
        newControlButton.StateCommon.Content.ShortText.TextH = ComponentFactory.Krypton.Toolkit.PaletteRelativeAlign.Near;
        newControlButton.TabIndex = 5;

        StringBuilder buttonText = new StringBuilder();
        buttonText.Append(Path.GetFileName(mtActivityControl.ControlName));
        /*if (mtActivityControl.SymbolicName.Length != 0)
        {
            buttonText.Append(\" (\");
            buttonText.Append(mtActivityControl.SymbolicName);
            buttonText.Append(\")\");
        }*/

        newControlButton.Text = buttonText.ToString();
        newControlButton.Values.ExtraText = \"\";
        newControlButton.Values.Image = null;
        newControlButton.Values.ImageStates.ImageCheckedNormal = null;
        newControlButton.Values.ImageStates.ImageCheckedPressed = null;
        newControlButton.Values.ImageStates.ImageCheckedTracking = null;
        newControlButton.Values.Text = buttonText.ToString();
        newControlButton.Click += new System.EventHandler(this.controlsButton_Click);
即使我的研究告诉我没有必要,我还是在Dispose函数中取消了如下事件:
newControlButton.Click -= new System.EventHandler(this.controlsButton_Click);
所以我的问题是: Krypton本身是否可能保留对我的控件的引用,从而导致某些内存未被释放(如果这是用于保留对象池的有限数量的内存,则可以,但是如果这是不受控制的内存泄漏)?如果它不是来自K,那么您是否有想法在哪里寻找可以正确销毁那些实例的方法? 非常感谢! 编辑: 我只是注意到类KryptonButtonEx不是来自Krypton,而是来自我的应用程序。但是我不认为它会改变问题,因为它唯一要做的就是覆盖GetPreferredSize函数:
/// <summary>
/// An extended/fixed KryptonButton which handles resizing correctly.
/// </summary>
public class KryptonButtonEx : ComponentFactory.Krypton.Toolkit.KryptonButton
{
    /// <summary>
    /// Gets the size of the preferred.
    /// </summary>
    /// <param name=\"proposedSize\">Size of the proposed.</param>
    /// <returns></returns>
    public override Size GetPreferredSize(Size proposedSize)
    {
        // Do we have a manager to ask for a preferred size?
        if (ViewManager != null)
        {
            // Ask the view to peform a layout
            Size retSize = ViewManager.GetPreferredSize(Renderer,proposedSize);

            // Apply the maximum sizing
            if (MaximumSize.Width > 0) retSize.Width = Math.Min(MaximumSize.Width,retSize.Width);
            if (MaximumSize.Height > 0) retSize.Height = Math.Min(MaximumSize.Height,retSize.Width);

            // Apply the minimum sizing
            if (MinimumSize.Width > 0) retSize.Width = Math.Max(MinimumSize.Width,retSize.Width);
            if (MinimumSize.Height > 0) retSize.Height = Math.Max(MinimumSize.Height,retSize.Height);

            return retSize;
        }
        else
        {
            // Fall back on default control processing
            return base.GetPreferredSize(proposedSize);
        }
    }
}
    

解决方法

        您发布了错误的代码。您应该对删除按钮的代码非常感兴趣,而不是创建按钮的代码。搜索Controls.Remove和Controls.Clear,并确保要删除的每个控件都已被处置。另一个诊断是TaskMgr.exe的“进程”选项卡。查看+选择列,然后勾选USER对象。看到这个数字稳定增长是一个很难知道代码没有将控件放置在应有位置的线索。这是我所知唯一不调用Dispose()导致永久泄漏的情况。 代替Controls.Clear(),使用如下代码:
 while (panel.Controls.Count > 0) panel.Controls[0].Dispose();
处置控件还会自动将其从父级的Controls集合中删除。     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...