如果我定义一个异常类
class Exception : public std::runtime_error
{
/*...*/
private:
std::exception_ptr next;
std::exception_ptr prev;
}
其中我指的是 next
未决异常和新抛出的异常,而 this
是 prev
未决异常,之后是否释放异常异常处理了吗?我问的原因是,我曾经听说 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/