- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 CF 应用程序,随着时间的推移会泄漏 UserControls。这花了一些时间,但我缩小了范围,甚至复制了完整框架 (3.5) 中的行为。由于两者都存在这种行为,我不想将其称为错误,但我确实不明白为什么会发生这种情况,希望有人能对此有所了解。
因此,我创建了一个简单的 WinForms 应用程序,其中包含一个表单和一个按钮。单击 Button 在创建新的 UserControl 和处理该控件之间交替。非常简单。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
UserControl1 m_ctl;
private void button1_Click(object sender, EventArgs e)
{
if (m_ctl == null)
{
m_ctl = new UserControl1();
m_ctl.Visible = true;
this.Controls.Add(m_ctl);
}
else
{
this.Controls.Remove(m_ctl);
m_ctl.Dispose();
m_ctl = null;
GC.Collect();
}
}
}
这是用户控件。它只是跟踪事件(即未最终确定)实例的数量。它上面什么都没有,只有一个标签,所以我可以目视确认它在表格上。
public partial class UserControl1 : UserControl
{
private static int m_instanceCount = 0;
public UserControl1()
{
var c = Interlocked.Increment(ref m_instanceCount);
Debug.WriteLine("Instances: " + c.ToString());
InitializeComponent();
}
~UserControl1()
{
var c = Interlocked.Decrement(ref m_instanceCount);
Debug.WriteLine("Instances: " + c.ToString());
}
}
这里的奇怪之处在于实例计数会无限增长。最终,在设备上,我的内存用完了。我想我也会在 PC 上使用,我只是不愿意点击明年的按钮。
现在,如果我像这样更改 UserControl 的默认设计器生成的 Dispose 方法,只需添加 ReRegisterForFinalize 调用:
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
if (disposing)
{
GC.ReRegisterForFinalize(this);
}
}
然后它的行为完全符合预期,在收集期间完成实例(手动或自动时)。
那么为什么会这样呢?显然基地正在调用 SuppressFinalize,但究竟为什么会发生这种情况,为什么以 Odin 的名义这是默认行为?
最佳答案
So why is this happening? Evidently the base is calling SuppressFinalize, but exactly why would this be happening, and why in the name of Odin is it the default behavior?
这是(正确地)实现 IDisposable
类的默认行为。当您调用 IDisposable.Dispose
时,默认的建议行为是抑制终结,因为终结的主要原因是清理从未处置的资源。这是因为终结是一项代价高昂的操作 - 您不想不必要地终结对象,如果调用了 Dispose
,则认为您已经清理了非托管资源。无论如何,任何托管内存都将得到处理。
您应该覆盖 Dispose
,并在 Dispose
覆盖范围内进行递减。
此行为在 documentation for IDisposable
中进行了解释.示例 Dispose
方法调用实现是(来自引用文档):
public void Dispose()
{
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time
GC.SuppressFinalize(this);
}
关于c# - GC 没有完成 UserControl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17331817/
您好,我的 WPF UserControl 知识只有一个小时了。因此,如果有很多关于这个问题的教程或/和答案,请原谅我(老实说,我认为这无法完成,需要重新编写代码……因此我想问的原因) 因此,在创建
我看过几篇文章介绍如何删除在运行时添加的 UserControl,但我的问题有点不同。我有一个 UserControl,它由一个图像组成,右上角有一个小“x”按钮,用于从其父 Canvas 中删除自身
我有一个用户控件 UserControl1,它在其资源中定义了一种样式。该用户控件包含一个 UserControl2 实例,它引用了该样式:
我正在尝试以 How can a WPF UserControl inherit a WPF UserControl? 中提到的方式继承 WPF 中的用户控件 namespace DMS.Presen
我想创建几个具有一些通用功能的 WPF UserControl 类。出于这个原因,我想从一个新的基类派生类,而这个基类又派生自 UserControl。 我的问题是我的 C# 类是 部分 定义的,而自
这个问题在这里已经有了答案: Should a user control have its own view model? (6 个答案) How to correctly bind to a de
我有一个 UserControl,它包含另一个带有 Button 的 UserControl。我想在第一个 UserControl(父级)中向该按钮添加一个事件。我尝试这样做: void Page_I
我需要我所有的用户控件都具有一些类似的功能。所以我的用户控件是: using System.Windows.Controls; namespace WpfApplication26 { ///
以下 WPF UserControl 调用了 DataTypeWholeNumber,它有效。 现在我想创建一个名为 DataTypeDateTime 和 DataTypeEmail 等的 UserC
我有一个包含按钮和其他一些控件的 UserControl: ... 当我创建该控件的新实例时,我想获取 Button 的 Command 属性: 当然,“Th
我不确定这个问题是否已经发布,并且我已经检查了建议的 SO 链接,但似乎没有一个链接与该问题完全相关。因此,如果有人认为这可能是重复的,请发布建议答案的链接并原谅这篇文章。 解决我的问题的方法可能不止
我在 UserControl_1 中有两个 UserControl,有一个按钮可以在 MainWindow.axml 中的 StackPanel 中添加 UserControl_2。我在 UserCo
我正在开发一个由多个传感器组成的模块化项目,每个传感器都继承自一个抽象的 Sensor 类。为了配置每个传感器,我已经开始为每个传感器添加一个用户控制面板(继承自 UserControl),它在运行时
我正在使用 c#.net 我的网络表单中有不同的 View ,除了三个文本框(到达/看到/离开时间)之外,这些通常都显示不同的信息。为了尝试减少代码,我创建了一个包含这三个文本框的 UserContr
我有一个 UserControl,它有两个组件 public System.Windows.Forms.ComboBox innerComboBox; public System.Windows.Fo
我有一个包含 ListBox 的 userControl。我想从另一个 userControl 访问那个 ListBox。 例如: UserControl1.ListBox1.Items.Count;
我在更新另一个用户控件中的用户控件时遇到问题。 示例代码: UserControl MyCart1 = (UserControl)Page.FindControl("MyCart1"); Update
我在下面添加了一些屏幕截图和重现步骤。 我的数据模型有一个基类,我们称它为 CommonThing,它有很多属性。然后有几个具有附加属性的此类的特殊版本。我现在想创建一个 UI 来将数据输入到这个模型
我有一个应用程序,我需要确保在使用 ShowDialog() 单击用户控件上的按钮打开的表单将在我处理用户控件时关闭和处理。 我通过计时器在主窗体中调用 userControl.Dispose()。
我正在努力实现... 用户控件(MyRepeater) Control Start Control End 页面 Page Start Page Item Tem
我是一名优秀的程序员,十分优秀!