gpt4 book ai didi

c# - Krypton(Winforms 库)是否可能存在内存泄漏问题

转载 作者:太空狗 更新时间:2023-10-30 01:09:44 25 4
gpt4 key购买 nike

我目前正在调试一个存在内存泄漏问题的大型 Winforms 应用程序。我使用 .NET 内存分析器,到目前为止我已经能够找到其中的一些泄漏并解决它们。但是现在我遇到了一个我不确定是不是问题的问题,如果是问题我也不知道如何解决。

在运行我的应用程序大约 1 分钟后(考虑到普通用户可以使用它几个小时,这不是很多),.NET 内存分析器向我显示了来自 Krypton Toolkit 的大约 100-200 个不同控件的实例,这个数字是如果我继续前进,就会增加(它们永远不会被垃圾收集,因为看起来它们仍在某处被引用)。现在,如果我检查这些实例的根路径,它们看起来都是这样的:

Root path for Krypton ViewManager

我不知道在哪里查看我的代码,以便在不再需要这些实例时正确取消引用它们,因为我不知道什么仍在引用该控件。我知道创建 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 本身是否有可能保留对我的控件的引用,这导致某些内存未被释放(如果它是用于保存对象池或类似东西的有限内存量,这可能没问题,但可能是如果它是一个不受控制的内存泄漏问题)?如果它不是来自 Krypton,您是否知道去哪里寻找正确销毁这些实例的想法?

非常感谢!

编辑:

我刚刚注意到 KryptonButtonEx 类不是来自 Krypton,而是来 self 的应用程序。但我认为它不会改变问题的任何内容,因为它唯一做的就是覆盖 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,进程选项卡。查看 + 选择列并勾选用户对象。看到这个数字稳步上升是一个确凿的线索,表明代码没有在应该的地方部署控件。这是我所知道的唯一没有调用 Dispose() 导致永久泄漏的情况。

代替 Controls.Clear(),使用如下代码:

 while (panel.Controls.Count > 0) panel.Controls[0].Dispose();

处置控件也会自动将其从父控件集合中删除。

关于c# - Krypton(Winforms 库)是否可能存在内存泄漏问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6151867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com