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

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

问题描述

我真的不敢相信我找不到这个...的清晰答案。

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

如何释放在C ++类构造函数之后分配的内存throw在使用 new 运算符初始化的情况下为异常。例如:

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

在调试时,我注意到控制器进入内存分配例程BEFORE它击中构造函数。

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

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


当new用于为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.

对于你的问题WHO删除内存的答案是后面的new-operator(由编译器生成)。如果它识别一个异常离开构造函数,它必须调用所有的类成员的析构函数(因为那些已经在调用构造函数代码之前已经成功构造)并释放它们的内存(可以与析构函数调用一起递归完成,很可能通过在它们上调用适当的 delete ),以及释放为这个类本身分配的内存。然后,它必须将构造函数中捕获的异常重新抛出给 new 的调用者。
当然可能有更多的工作,必须做,但我不能拉出所有的细节从我的头,因为他们是由每个编译器的实现。

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天全站免登陆