谁删除在“新的”期间分配的存储器在构造函数中有异常的操作? [英] Who deletes the memory allocated during a "new" operation which has exception in constructor?

查看:142
本文介绍了谁删除在“新的”期间分配的存储器在构造函数中有异常的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不能相信我找不到一个明确的答案...

I really can't believe I couldn't find a clear answer to this...

如何释放在C ++类构造函数抛出后分配的内存在使用运算符进行初始化的情况下是一个例外。例如:

How do you free the memory allocated after a C++ class constructor throws an exception, in the case where it's initialised using the new operator. E.g.:

class Blah
{
public:
  Blah()
  {
    throw "oops";
  }
};

void main()
{
  Blah* b = NULL;
  try
  {
    b = new Blah();
  }
  catch (...)
  {
    // What now?
  }
}

当我尝试这个, b 在catch块中为NULL(这是有道理的)。

When I tried this out, b is NULL in the catch block (which makes sense).

当调试时,我注意到conrol进入内存分配程序它触及构造函数。

When debugging, I noticed that the conrol enters the memory allocation routine BEFORE it hits the constructor.

在MSDN网站上似乎确认这一点


当新的用于为C ++类对象分配内存
时,对象的
构造函数在内存
被分配之后被调用。

When new is used to allocate memory for a C++ class object, the object's constructor is called after the memory is allocated.

所以,请记住局部变量从未分配 b (即在catch块中为NULL)如何删除分配的内存?

So, bearing in mind that the local variable b is never assigned (i.e. is NULL in the catch block) how do you delete the allocated memory?

在这个平台上获得跨平台的答案也是很好的。即,C ++规范说的是什么?

It would also be nice to get a cross platform answer on this. i.e., what does the C++ spec say?

澄清:我不是在讨论类在c'tor中分配内存本身然后抛出的情况。我明白,在这些情况下,不要打电话。我正在谈论用于在我的情况下分配 对象( Blah )的内存。

CLARIFICATION: I'm not talking about the case where the class has allocated memory itself in the c'tor and then throws. I appreciate that in those cases the d'tor won't be called. I'm talking about the memory used to allocate THE object (Blah in my case).

推荐答案

您应该参考类似的问题这里此处
基本上如果构造函数抛出一个异常,那么安全的是,对象本身的内存被再次释放。虽然在构造函数中已经声明了其他内存,但是在离开构造函数之前,您可以自由释放它。

You should refer to the similar questions here and here. Basically if the constructor throws an exception you're safe that the memory of the object itself is freed again. Although, if other memory has been claimed during the constructor, you're on your own to have it freed before leaving the constructor with the exception.

对于您删除的问题内存的答案是新操作符(由编译器生成)后面的代码。如果它识别出构造函数的异常,它必须调用类成员的所有析构函数(因为在调用构造函数代码之前已经构建成功)并释放其内存(可以与析构函数调用一起递归地完成)通过调用它们上的正确的删除),以及释放为该类本身分配的内存。那么它必须将捕获的异常从构造函数重新发送到新的的调用者。
当然可能有更多的工作需要完成,但我无法从我的头上提取所有的细节,因为它们符合每个编译器的实现。

For your question WHO deletes the memory the answer is the code behind the new-operator (which is generated by the compiler). If it recognizes an exception leaving the constructor it has to call all the destructors of the classes members (as those have already been constructed successfully prior calling the constructor code) and free their memory (could be done recursively together with destructor-calling, most probably by calling a proper delete on them) as well as free the memory allocated for this class itself. Then it has to rethrow the catched exception from the constructor to the caller of new. Of course there may be more work which has to be done but I cannot pull out all the details from my head because they are up to each compiler's implementation.

这篇关于谁删除在“新的”期间分配的存储器在构造函数中有异常的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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