• Please avoid implement C# Destruction


    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.

  • 相关阅读:
    Namenode主节点停止报错 Error: flush failed for required journal
    IntelliJ IDEA2018.3 最新破解方法
    idea 中解决maven 包冲突的问题(maven helper)
    java中的守护线程
    synchronized锁住的是代码还是对象
    maven package,clean,install,compile命令
    设计模式——装饰者模式
    设计模式——观察者模式
    com.alibaba.fastjson.JSON对类对象的序列化与反序列化
    java8 Stream使用案例
  • 原文地址:https://www.cnblogs.com/anders06/p/1425998.html
Copyright © 2020-2023  润新知