【发布时间】:2011-10-31 21:28:18
【问题描述】:
根据这篇文章,MSDN上的http://msdn.microsoft.com/en-us/library/ms177197.aspx,我们应该释放终结器中的非托管资源和析构器中的托管资源。 IE。以下模式:
// destructors_finalizers_1.cpp
// compile with: /clr /c
ref struct A {
// destructor cleans up all resources
~A() {
// clean up code to release managed resource
// ...
// to avoid code duplication
// call finalizer to release unmanaged resources
this->!A();
}
// finalizer cleans up unmanaged resources
// destructor or garbage collector will
// clean up managed resources
!A() {
// clean up code to release unmanaged resource
// ...
}
};
为什么不把它全部放在析构函数中并废弃终结器呢?是不是因为垃圾收集器仍有可能不确定地清理对象?
【问题讨论】:
标签: c++-cli