Windows 窗体关闭但未销毁

发布于 2024-10-12 05:57:18 字数 129 浏览 3 评论 0原文

在我的 Windows 窗体应用程序中,当我关闭窗体(派生自基本窗体)时,其 FormClosing 和 FormClosed 事件会触发,但析构函数永远不会触发。它仍然保持内存被占用。

关于如何在关闭时完全破坏表单的任何想法?

In my Windows Form application when I close a form (which is derived from a base form), its FormClosing and FormClosed Events fire but destructor never fires off. It still keeps the memory occupied.

Any ideas on how to destroy the form completely when it is closed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

穿透光 2024-10-19 05:57:18

如果它没有被销毁,则意味着垃圾收集器认为它不应该被销毁。

这基本上意味着您可以:

  1. 在某处保存对对象的引用
  2. 让对象侦听事件(这也是对对象的一种引用)

垃圾收集器不会释放表单,直到没有对它的引用。

如果您有想要处置的重要资源,请将其设为IDisposable,并使用Dispose 方法。

If it's not destroyed that means the Garbage Collector doesn't think it should be destroyed.

This basically means that you're either:

  1. Holding a reference to the object somewhere
  2. Have the object listening to an event (That's also a kind of reference to the object)

The garbage collector won't free the Form until there are no references to it.

If you have important resources you want to dispose of, make it IDisposable, and use the Dispose method.

葬シ愛 2024-10-19 05:57:18

析构函数(或更准确地说,终结器 - .NET 中没有析构函数)不保证在 .NET 中执行 - 对象可能会在运行时的突发奇想中被清理甚至永远不会。你不能依赖你的终结器方法被调用。

如果您需要在表单关闭时执行某些操作,请处理 Closed 事件。

如果您需要释放非托管资源(例如关闭打开的文件),请将此逻辑添加到 Dispose() 方法中。

如果您担心内存使用,请不要担心内存使用。运行时根据自己的逻辑自动管理内存。

参考:垃圾收集 (MSDN)

Destructors (or more correctly, finalizers - there are no destructors in .NET) are not guaranteed to be executed in .NET - objects may be cleaned up at the whim of the runtime or even never. You cannot rely on your finalizer method ever being called.

If you need to do something when your form is closed, handle the Closed event.

If you need to release an unmanaged resource (e.g. close an open file), add this logic to the Dispose() method.

If you are worried about memory use, do not worry about memory use. The runtime manages memory automatically based on its own logic.

Reference: Garbage Collection (MSDN)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文