none
关于析构函数问题 RRS feed

  • 问题

  •  

    在c++/cli中,有~Class()和!Class()两种析构函数,~Class()会被编译成Dispose(),而!Class()会被编译成Finalize(),不知道我的理解对吗?

    另外,Finalize()可以通过什么方式来调用?在什么情况下会被调用?

    谢谢指点

    2008年12月2日 5:17

答案

  • Destructors and Finalizers in Visual C++

     

    Code Snippet
    class classname {   
      ~classname() {}   // destructor   
      !classname() {}   // finalizer
    };

     

     
    The common language runtime's garbage collector deletes unused managed objects and releases their memory when no longer needed. However, a type may use resources that the garbage collector does not know how to release. These resources are known as unmanaged resources (native file handles, for example). You should release all unmanaged resources in the finalizer. Because managed resources are released non-deterministically by the garbage collector, it is not safe to refer to managed resources in a finalizer because it is possible that the garbage collector has already cleaned up that managed resource.  A Visual C++ finalizer is not the same as the Finalize method (common language runtime documentation uses finalizer and the Finalize method synonymously). The Finalize method is called by the garbage collector, which invokes each finalizer in a class inheritance chain. Unlike Visual C++ destructors, calling a derived class finalizer does not cause the compiler to invoke the finalizer in all base classes. 
     
    2008年12月4日 8:07
  •  Xiaoyun Li - MSFT 写:

    Destructors and Finalizers in Visual C++

     

    Code Snippet
    class classname {   
      ~classname() {}   // destructor   
      !classname() {}   // finalizer
    };

     

     
    The common language runtime's garbage collector deletes unused managed objects and releases their memory when no longer needed. However, a type may use resources that the garbage collector does not know how to release. These resources are known as unmanaged resources (native file handles, for example). You should release all unmanaged resources in the finalizer. Because managed resources are released non-deterministically by the garbage collector, it is not safe to refer to managed resources in a finalizer because it is possible that the garbage collector has already cleaned up that managed resource. 
     
    A Visual C++ finalizer is not the same as the Finalize method (common language runtime documentation uses finalizer and the Finalize method synonymously). The Finalize method is called by the garbage collector, which invokes each finalizer in a class inheritance chain. Unlike Visual C++ destructors, calling a derived class finalizer does not cause the compiler to invoke the finalizer in all base classes. 
     

     

    哥们,说中文,OK? 开个玩笑。

     

    Finalize应该是被GC调用的,GC经过垃圾回收以后,维护一个Finalize队列,Jeffery Richater在MSDN上有一篇文章讲解GC的工作原理。

    2008年12月5日 11:25

全部回复

  • 是不是系统进行垃圾回收的时候会调用Finalize()方法。

    2008年12月2日 8:00
    版主
  • Destructors and Finalizers in Visual C++

     

    Code Snippet
    class classname {   
      ~classname() {}   // destructor   
      !classname() {}   // finalizer
    };

     

     
    The common language runtime's garbage collector deletes unused managed objects and releases their memory when no longer needed. However, a type may use resources that the garbage collector does not know how to release. These resources are known as unmanaged resources (native file handles, for example). You should release all unmanaged resources in the finalizer. Because managed resources are released non-deterministically by the garbage collector, it is not safe to refer to managed resources in a finalizer because it is possible that the garbage collector has already cleaned up that managed resource.  A Visual C++ finalizer is not the same as the Finalize method (common language runtime documentation uses finalizer and the Finalize method synonymously). The Finalize method is called by the garbage collector, which invokes each finalizer in a class inheritance chain. Unlike Visual C++ destructors, calling a derived class finalizer does not cause the compiler to invoke the finalizer in all base classes. 
     
    2008年12月4日 8:07
  •  Xiaoyun Li - MSFT 写:

    Destructors and Finalizers in Visual C++

     

    Code Snippet
    class classname {   
      ~classname() {}   // destructor   
      !classname() {}   // finalizer
    };

     

     
    The common language runtime's garbage collector deletes unused managed objects and releases their memory when no longer needed. However, a type may use resources that the garbage collector does not know how to release. These resources are known as unmanaged resources (native file handles, for example). You should release all unmanaged resources in the finalizer. Because managed resources are released non-deterministically by the garbage collector, it is not safe to refer to managed resources in a finalizer because it is possible that the garbage collector has already cleaned up that managed resource. 
     
    A Visual C++ finalizer is not the same as the Finalize method (common language runtime documentation uses finalizer and the Finalize method synonymously). The Finalize method is called by the garbage collector, which invokes each finalizer in a class inheritance chain. Unlike Visual C++ destructors, calling a derived class finalizer does not cause the compiler to invoke the finalizer in all base classes. 
     

     

    哥们,说中文,OK? 开个玩笑。

     

    Finalize应该是被GC调用的,GC经过垃圾回收以后,维护一个Finalize队列,Jeffery Richater在MSDN上有一篇文章讲解GC的工作原理。

    2008年12月5日 11:25
  •  Xiaoyun Li - MSFT 写:

    Destructors and Finalizers in Visual C++

     

    Code Snippet
    class classname {   
      ~classname() {}   // destructor   
      !classname() {}   // finalizer
    };

     

     
    The common language runtime's garbage collector deletes unused managed objects and releases their memory when no longer needed. However, a type may use resources that the garbage collector does not know how to release. These resources are known as unmanaged resources (native file handles, for example). You should release all unmanaged resources in the finalizer. Because managed resources are released non-deterministically by the garbage collector, it is not safe to refer to managed resources in a finalizer because it is possible that the garbage collector has already cleaned up that managed resource.  A Visual C++ finalizer is not the same as the Finalize method (common language runtime documentation uses finalizer and the Finalize method synonymously). The Finalize method is called by the garbage collector, which invokes each finalizer in a class inheritance chain. Unlike Visual C++ destructors, calling a derived class finalizer does not cause the compiler to invoke the finalizer in all base classes. 
     

    以下盗版自原话:

    CLR垃圾收集器删除不再需要的非托管对象并释放占用内存。然而,还可能存在着一种垃圾收集器不知道怎样释放的资源。这些资源已知是非托管的(例如常规文件句柄)。你应当在finalizer(标准析构函数别名)里释放所有非托管资源。因为垃圾收集器会不确定地释放受控资源。在finalizer里引用受控资源是不安全的,这是因为受控资源有可能已被垃圾收集器清除。Visual C++ finalizer 和Finalize方法(clr文档同时认可使用finalizer和Finalize方法)不一样。而Finalize方法(每个Finalize方法都挂在每个类继承链中)被垃圾收集器调用。不像Visual C++析构函数那样调用派生类的析构函数会让编译器将各基类的析构函数都被联入调用。

    2008年12月12日 3:55