c++ - 是否可以使用 std::exception_ptr 创建内存泄漏

标签 c++ c++11 exception nested-exceptions

如果我定义一个异常类

class Exception : public std::runtime_error
{
/*...*/
private:
    std::exception_ptr next;
    std::exception_ptr prev;
}

其中我指的是 next 未决异常和新抛出的异常,而 thisprev 未决异常,之后是否释放异常异常处理了吗?我问的原因是,我曾经听说 std::exception_ptr 是根据引用计数实现的,如果我在这个类中生成引用 cicles,这可能会导致内存泄漏。

最佳答案

The reason I'm asking is, that I once heard that std::exception_ptr is implemented in terms of reference counting [...]

不一定,但有可能。 C++11 标准的第 18.8.5/6 段在注释中提到了这一点:

typedef unspecified exception_ptr;

[...]

6 [ Note: An implementation might use a reference-counted smart pointer as exception_ptr. —end note ]

因此,您可能不想建立exception_ptr 的循环。

关于c++ - 是否可以使用 std::exception_ptr 创建内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16915796/

相关文章:

c++ - 不同 Observable 的观察者模式

c++ - 使用 gcc 在 Linux 上运行线程构建 block (Intel TBB)

c++ - 使用 auto 作为迭代器

java - 构造函数中有多个参数的异常

使用 memcached 的 PHP 扩展

c++ - 在 RHEL6 上构建 GCC 5.2

c++ - 捕获微不足道的 boost 异常的奇怪问题

c++ - 如何在匿名命名空间中使用带有免费功能的谷歌测试?

java - web.xml错误500和struts全局异常有什么区别?

c# - 远程服务器返回错误 : (500) Internal Server Error. GetRequest() C#