下面的代码是否会导致 C++ 中的内存泄漏 [英] Will the below code cause memory leak in c++

查看:27
本文介绍了下面的代码是否会导致 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:

  1. 正在构造的对象的析构函数将不会被调用.
  2. 将调用包含在该对象类中的成员对象的析构函数
  3. 正在构造的对象的内存将被释放.

这意味着如果你的对象拥有资源,你有两种方法可以用来清理那些在构造函数抛出时可能已经获得的资源:

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:

  1. 捕获异常,释放资源,然后重新抛出.这可能很难纠正,并可能成为维护问题.
  2. 使用对象来管理资源生命周期 (RAII) 并将这些对象用作成员.当对象的构造函数抛出异常时,成员对象将调用析构函数,并有机会释放它们负责的生命周期的资源.

这篇关于下面的代码是否会导致 C++ 中的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆