简体   繁体   English

如果构造函数抛出异常,new 分配的内存会发生什么?

[英]What happens to the memory allocated by `new` if the constructor throws?

Will this code cause a memory leak?这段代码会导致内存泄漏吗?

#include <stdexept>

class MyClass
{
public:
    MyClass()
    {
        throw std::runtime_error("Test");
    }
};

int main()
{
    try
    {
        MyClass * myClass = new MyClass;
    }
    catch (const std::exception & exc)
    {
        // Memory leak?
    }
    return 0;
}

The memory allocated by new is never deleted. new分配的内存永远不会被删除。 Is this taken care of internally, or it an actual memory leak?这是在内部处理的,还是实际的内存泄漏?

The memory will be automatically freed before the exception propagates.在异常传播之前,内存将被自动释放。

This is essential, because a) the program never receives a pointer to free, and b) even if it did, it would have no portable way to actually free it since the memory never became an object that you can delete.这是必不可少的,因为 a) 程序永远不会收到指向 free 的指针,并且 b) 即使收到了,它也没有可移植的方式来实际释放它,因为内存永远不会成为您可以删除的对象。

The memory will be properly deallocated.内存将被正确释放。

Related questions at SO. SO的相关问题。

  • Is it ever not safe to throw an exception in a constructor? 在构造函数中抛出异常是否不安全?
  • C++ : handle resources if constructors may throw exceptions (Reference to FAQ 17.4) C++:如果构造函数可能抛出异常,则处理资源(参考 FAQ 17.4)
  • prasoon@prasoon-desktop ~ $ cat noleak.cpp && g++ noleak.cpp && valgrind --leak-check=full ./a.out
    #include <stdexcept>
    
    class MyClass
    {
    public:
        MyClass()
        {
            throw std::runtime_error("Test");
        }
    };
    
    int main()
    {
        try
        {
            MyClass * myClass = new MyClass;
        }
        catch (const std::exception & exc)
        {
            // Memory leak?
        }
        return 0;
    }
    ==3652== Memcheck, a memory error detector
    ==3652== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
    ==3652== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
    ==3652== Command: ./a.out
    ==3652== 
    ==3652== 
    ==3652== HEAP SUMMARY:
    ==3652==     in use at exit: 0 bytes in 0 blocks
    ==3652==   total heap usage: 3 allocs, 3 frees, 106 bytes allocated
    ==3652== 
    ==3652== All heap blocks were freed -- no leaks are possible
    ==3652== 
    ==3652== For counts of detected and suppressed errors, rerun with: -v
    ==3652== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 17 from 6)
    prasoon@prasoon-desktop ~ $ 
    

    $15.2/2 - “部分构造或部分销毁的对象将为其所有完全构造的基类和非变体成员执行析构函数,即,对于主构造函数(12.6.2)已完成执行的子对象并且析构函数尚未开始执行。类似地,如果对象的非委托构造函数已完成执行并且该对象的委托构造函数以异常退出,则将调用该对象的析构函数。如果该对象是在新分配的-expression,匹配的释放函数(3.7.4.2, 5.3.4, 12.5),如果有的话,会被调用来释放对象占用的存储空间。

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

    相关问题 如果在释放新操作员分配的内存之前发生异常会怎样? - What will happens if before freeing memory allocated by new operator, exception occurred? 全局分配的内存会发生什么? - What happens to globally allocated memory? 如果构造函数抛出异常会发生什么? - What happens if a constructor throws an exception? 当使用其他内存的new运算符更改指向已分配内存的ptr时,会发生什么情况? - What happens, when changing ptr which is pointing to allocated memory using operator new to some other memory? 使用函数分配内存时会发生什么? - What happens when Memory is allocated using a function? 当构造函数抛出异常时,构造函数中分配的内存是如何释放的? - How is memory allocated in constructor released when the constructor throws an exception? 在构造“新”对象期间发生异常时,堆分配的内存将如何处理? - What happens to heap allocated memory when exceptions occur during the constructing 'new' objects? 向未分配的内存分配值时会发生什么? - What happens when assigning a value to non-allocated memory? 在范围结束后,静态分配的内存会发生什么? - What happens to statically allocated memory after its scope ends? 动态分配的内存构造函数 - Dynamically Allocated Memory Constructor
     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM