惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
博客园 - 【当耐特】
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
aimingoo的专栏
aimingoo的专栏
WordPress大学
WordPress大学
G
Google Developers Blog
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
P
Proofpoint News Feed
J
Java Code Geeks
U
Unit 42
云风的 BLOG
云风的 BLOG
阮一峰的网络日志
阮一峰的网络日志
N
Netflix TechBlog - Medium
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
D
Docker
V
Visual Studio Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Help Net Security
V
V2EX
T
Tailwind CSS Blog

博客园 - Anders06

A memory leak issue with WPF Command Binding 我也谈面试 附赠一份题目 败给了IEqualityComparer 标注随测量物体旋转问题 How to detect memory leak issue 图纸中角度标注算法 认真的程序员最可爱 Clean Code反案例:什么是不好的API 避免陷阱,重写Equals方法您需要注意的其中2个原则 What’s the problems with the following code 对象池 关于设计和设计文档的2个补充 SelectMany ConverAll & Exists SOS:利用WPF RichTextBox 提取RTF, 莫名丢掉了Underline样式信息 想探讨两个关于设计文档问题 Don‘t Cry for Me, Argentina 拒绝高姿态 SOS: How to popup a HwndSource on topmost
Please avoid implement C# Destruction
Anders06 · 2009-03-31 · via 博客园 - Anders06

I remember in the attach mail I gave my suggestions about how to use Destruction in C#, but it seems it didn’t catch your eyes.

So I would like to give my kindly suggestions and remind you again.

Before I debate with you,  please have a look at this article: Dispose & Finalize

Ø Tip from the author said:

· Don't create empty destructors. In other words, you should never explicitly define a destructor unless your class needs to clean up unmanaged resources—and if you do define one, it should do some work. If, later, you no longer need to clean up unmanaged resources in the destructor, remove it altogether”.

One more, in this article Finalization section,  Jeffrey Richter said:

· “let me warn you right now: object finalization and destructors have very different semantics and it is best to forget everything you know about destructors when thinking about finalization.”

· “When designing a type it is best to avoid using a Finalize method.”

He also listed many reasons to explain why we should avoid to use a Finalize method in that article.

So, Destruction in C# works total different with Destruction in C++.

And there are two formats misuse C# Destruction in our project, there are:

Format 1:

       ~DisplaySettingsListener()

       {

       }

Format 2:

       ~DisplaySettingsListener()

       {

            UnAdvise(); // un-register the events

        }

Format 1 is empty destructor which will has some performance issue. (The DisplaySettingsListener object requires at least two garbage collections, detail information please refer to MSDN from here.)

Format 2 try to un-register events in destructor,  but it never works as your expect. Why? The even bad news is that this format misuse of Destruction is one important killer who caused the document could not been disposed when we close the document in our product.

Seeing is believing,  let me show you a simple example to explain why the format 2 doesn’t make sense.

Code

If you are interested on it, please copy the above code, and run it  on your computer.

What do you see? Shocked at it?

Shows the following figure from my side:

clip_image003

The results show that the byte array resource were NEVER collected, Un-register event in Destruction here doesn’t make sense at all.

So how to fix this problem?  In fact, I also have gave my solution in the attached mail, the key point is implement IDispose interface or implement Dispose Pattern, detail information please refer to this article.