我运行了一个示例程序,确实调用了堆栈分配对象的析构函数,但这是否由标准保证?
我对C++/CLI中的资源管理非常困惑.我认为我有一个句柄(没有任何双关语),但我偶然发现了整个auto_gcroot<T>课程,同时查看了头文件,导致谷歌搜索,然后是阅读文档的更好部分,现在混乱.所以我想我会转向社区.
我的问题涉及auto_handle/stack语义和auto_gcroot/gcroot之间的区别.
auto_handle:我的理解是这将清理托管函数中创建的托管对象.我的困惑是,垃圾收集者不应该为我们这样做吗?这不是托管代码的重点吗?更具体:
//Everything that follows is managed code
void WillThisLeak(void)
{
String ^str = gcnew String ^();
//Did I just leak memory? Or will GC clean this up? what if an exception is thrown?
}
void NotGoingToLeak(void)
{
String ^str = gcnew String^();
delete str;
//Guaranteed not to leak, but is this necessary?
}
void AlsoNotGoingToLeak(void)
{
auto_handle<String ^> str = gcnew String^();
//Also Guaranteed not to leak, but is this necessary?
}
void DidntEvenKnowICouldDoThisUntilToday(void)
{
String …Run Code Online (Sandbox Code Playgroud)