这段代码
#include <iostream>
#include <memory>
template <typename T>
class ScopeGuard final {
public:
explicit ScopeGuard(T callback) : callback_(std::move(callback)) {}
ScopeGuard(const ScopeGuard&) = delete;
ScopeGuard(ScopeGuard&&) = delete;
ScopeGuard& operator=(const ScopeGuard&) = delete;
ScopeGuard& operator=(ScopeGuard&&) = delete;
~ScopeGuard() noexcept(false) { callback_(); }
private:
T callback_;
};
static int a_instances = 0;
struct A {
A() {
++a_instances;
std::cout << "ctor" << std::endl;
}
A(A&&) = delete;
A(const A&) = delete;
~A() {
--a_instances;
std::cout << "dtor" << std::endl;
}
};
struct Res {
std::shared_ptr<A> shared;
explicit Res(std::shared_ptr<A> shared) : shared(shared) {
std::cout << "res ctor" << std::endl;
}
Res(Res&& o) : shared(std::move(o.shared)) {
std::cout << "res mv" << std::endl;
}
Res(const Res& o) : shared(o.shared) { std::cout << "res cp" << std::endl; }
~Res() { std::cout << "res dtor" << std::endl; }
};
Res LeakImpl() {
ScopeGuard on_exit{[] { throw std::runtime_error("error"); }};
Res res{std::make_shared<A>()};
return res;
}
void Leak() {
try {
auto res = LeakImpl();
} catch (const std::exception& e) {
std::cout << e.what() << std::endl;
}
}
int main() {
Leak();
if (a_instances) {
std::cout << "leak, instances count: " << a_instances << std::endl;
}
return 0;
}
输出这个(GCC 9.1、GCC 11.1 和 Clang 9.0.1)ctor
res ctor
error
leak, instances count: 1
这意味着 A::~A()
,也不是 Res::~Res()
被调用,导致这种情况下内存泄漏。我想在里面
LeakImpl()
Res::~Res()
之前没有被调用过 ScopeGuard::~ScopeGuard
因为复制省略,但为什么在从 ScopeGuard::~ScopeGuard
抛出异常时不调用它左 try
堵塞?如果我改变
LeakImpl()
像这样:Res LeakImpl() {
ScopeGuard on_exit{[] { throw std::runtime_error("error"); }};
return Res{std::make_shared<A>()};
}
然后 GCC 11.1 生成二进制输出:ctor
res ctor
res dtor
dtor
error
无泄漏而在 GCC 9.1 和 Clang 9.0.1 上,错误仍然存在。
对我来说,这看起来像是编译器中的一个错误,你有什么想法?
最佳答案
似乎是一个错误,已经为 gcc 报告和修复:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66139
我想,std::shared_ptr<>
在异常抛出期间部分创建
作为一种解决方法,您可以防止复制省略:
Res LeakImpl() {
ScopeGuard on_exit{[] { throw std::runtime_error("error"); }};
Res res{std::make_shared<A>()};
return std::move(res);
}
或创建 ScopeGuard
之前 Res
(在这种情况下,销毁顺序很重要):Res LeakImpl() {
Res res{std::make_shared<A>()};
ScopeGuard on_exit{[] { throw std::runtime_error("error"); }};
return res;
}
关于c++ - 抛出析构函数导致内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68196695/