相关疑难解决方法(0)

在C++中抛出后是否会调用析构函数?

我运行了一个示例程序,确实调用了堆栈分配对象的析构函数,但这是否由标准保证?

c++ exception-handling raii try-catch

43
推荐指数
2
解决办法
2万
查看次数

C++/CLI资源管理混淆

我对C++/CLI中的资源管理非常困惑.我认为我有一个句柄(没有任何双关语),但我偶然发现了整个auto_gcroot<T>课程,同时查看了头文件,导致谷歌搜索,然后是阅读文档的更好部分,现在混乱.所以我想我会转向社区.

我的问题涉及auto_handle/stack语义和auto_gcroot/gcroot之间的区别.

  1. 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)

interop memory-management c++-cli

14
推荐指数
1
解决办法
3687
查看次数