gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:54:03 27 4
gpt4 key购买 nike

如果我定义一个异常类

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com