Will the below code cause memory leak in c++(下面的代码是否会导致 C++ 中的内存泄漏)
问题描述
class someclass {};
class base
{
int a;
int *pint;
someclass objsomeclass;
someclass* psomeclass;
public:
base()
{
objsomeclass = someclass();
psomeclass = new someclass();
pint = new int();
throw "constructor failed";
a = 43;
}
}
int main()
{
base temp();
}
在上面的代码中,构造函数抛出.哪些对象会被泄露,如何避免内存泄露?
In the above code, the constructor throws. Which objects will be leaked, and how can the memory leaks be avoided?
int main()
{
base *temp = new base();
}
上面的代码怎么样?构造函数抛出后如何避免内存泄漏?
How about in the above code? How can the memory leaks be avoided after the constructor throws?
推荐答案
是的,它会泄漏内存.当构造函数抛出时,不会调用任何析构函数(在这种情况下,您不会显示释放动态分配对象的析构函数,但假设您有一个析构函数).
Yes it will leak memory. When the constructor throws, no destructor will be called (in this case you don't show a destructor that frees the dynamically allocated objects, but lets assume you had one).
这是使用智能指针的一个主要原因——因为智能指针是成熟的对象,它们将在异常堆栈展开期间调用析构函数并有机会释放内存.
This is a major reason to use smart pointers - since the smart poitners are full fledged objects, they will get destructors called during the exception's stack unwind and have the opportunity to free the memory.
如果你使用类似 Boost 的 scoped_ptr<> 模板,你的类可能看起来更像:
If you use something like Boost's scoped_ptr<> template, your class could look more like:
class base{
int a;
scoped_ptr<int> pint;
someclass objsomeclass;
scoped_ptr<someclass> psomeclass;
base() :
pint( new int),
objsomeclass( someclass()),
psomeclass( new someclass())
{
throw "constructor failed";
a = 43;
}
}
而且你不会有内存泄漏(默认的 dtor 也会清理动态内存分配).
And you would have no memory leaks (and the default dtor would also clean up the dynamic memory allocations).
总结一下(希望这也回答了有关
To sum up (and hopefully this also answers the question about the
base* temp = new base();
声明):
当在构造函数中抛出异常时,在正确处理对象构造中止时可能发生的资源分配方面,您应该注意以下几点:
When an exception is thrown inside a constructor there are several things that you should take note of in terms of properly handling resource allocations that may have occured in the aborted construction of the object:
- 正在构造的对象的析构函数将不会被调用.
- 将调用包含在该对象类中的成员对象的析构函数
- 正在构造的对象的内存将被释放.
这意味着如果你的对象拥有资源,你有两种方法可以用来清理那些在构造函数抛出时可能已经获得的资源:
This means that if your object owns resources, you have 2 methods available to clean up those resources that might have already been acquired when the constructor throws:
- 捕获异常,释放资源,然后重新抛出.这可能很难纠正,并可能成为维护问题.
- 使用对象来管理资源生命周期 (RAII) 并将这些对象用作成员.当对象的构造函数抛出异常时,成员对象将调用析构函数,并有机会释放它们负责的生命周期的资源.
这篇关于下面的代码是否会导致 C++ 中的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:下面的代码是否会导致 C++ 中的内存泄漏
基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07