为什么我的析构函数从不运行?

Why does my destructor never run?(为什么我的析构函数从不运行?)

本文介绍了为什么我的析构函数从不运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有析构方法的空白 Winform

public 部分类 Form1 : Form{公共表格1(){System.Diagnostics.Trace.WriteLine("Form1.Initialize " + this.GetHashCode().ToString());初始化组件();}~Form1(){System.Diagnostics.Trace.WriteLine("Form1.Dispose" + this.GetHashCode().ToString());}}

当表单被销毁时,我希望它写入输出窗口:

<上一页>(Form1打开)Form1.初始化 41149443(Form1 关闭)Form1.Dispose 41149443

MSDN 提出了 3 种实现析构函数的方法:

  • ~析构函数()http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx

  • 一次性http://msdn.microsoft.com/en-us/library/system.idisposable.aspx

  • SafeHandle 模式http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.safehandle.aspx

但是,这些方法都没有将Form1.Dispose 41149443"写入输出窗口.因此,我无法判断表格是否已被销毁.建议?

由于垃圾收集器的不确定性,我是否应该放弃实现这一目标的希望?

有没有其他方法可以知道 Form1 是否被垃圾回收了?

解决方案

您列出的实现析构函数的三种方法中只有一种实际上涉及析构函数,那就是 ~Destructor().

如果您实现 IDisposable 并处置您的对象,那么 Dispose 中的代码将运行,但没有理由认为您的析构函数会运行.

我认为你在这里追逐不可能的事情.析构函数按照垃圾收集器的命令运行.这不是你可以控制的东西.GC 完全有权形成运行析构函数只是浪费时间的观点,如果有足够的内存,它就会形成这种观点.

如果您需要可预测的处置、最终确定等,请使用 IDisposable.

I have a blank Winform with a destructor method

public partial class Form1 : Form
{
    public Form1()
    {
        System.Diagnostics.Trace.WriteLine("Form1.Initialize " + this.GetHashCode().ToString());
        InitializeComponent();
    }
    ~Form1()
    {
        System.Diagnostics.Trace.WriteLine("Form1.Dispose " + this.GetHashCode().ToString());
    }
}

When the form is destroyed, I want it to write to the output window:

(Form1 opened)
Form1.Initialize 41149443
(Form1 closed)
Form1.Dispose 41149443

MSDN suggests 3 ways in implementing destructor:

  • ~Destructor() http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx

  • IDisposable http://msdn.microsoft.com/en-us/library/system.idisposable.aspx

  • SafeHandle pattern http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.safehandle.aspx

However, none of these ways write "Form1.Dispose 41149443" to the output Window. Therefore, I am not able to tell whether the form has been destroyed or not. Suggestions ?

Should I give up hope on achieving this due to uncertainty of garbage collector?

Is there another way to know whether Form1 has been garbage collected ?

解决方案

Only one of the three ways to implement a destructor that you list actually involves a destructor, and that's ~Destructor().

If you implement IDisposable, and dispose of your object, then the code in Dispose will run, but there's no reason to think that your destructor will.

I think you chasing the impossible here. Destructors run as and when the garbage collector so decrees. It's not something that you have any control over. The GC is well within its rights to form the opinion that running destructors simply wastes time, and if there is plenty of memory it will form that opinion.

If you need predictable disposal, finalization etc., then use IDisposable.

这篇关于为什么我的析构函数从不运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么我的析构函数从不运行?

基础教程推荐