問題描述
我真的不敢相信我找不到明確的答案...
在使用 new
運算符初始化的情況下,如何釋放 C++ 類構造函數拋出異常后分配的內存.例如:
class 廢話{上市:廢話(){拋出哎呀";}};無效主(){廢話* b = NULL;嘗試{b = new Blah();}抓住 (...){//現在怎么辦?}}
當我嘗試這樣做時,catch 塊中的 b
為 NULL(這是有道理的).
在調試時,我注意到控制在命中構造函數之前進入內存分配例程.
MSDN 網站上的這一點似乎證實了這一點::><塊引用>
當new用于分配內存時對于 C++ 類對象,對象的構造函數在內存之后被調用已分配.
所以,記住局部變量 b
永遠不會被賦值(即在 catch 塊中為 NULL)你如何刪除分配的內存?
如果能得到一個跨平臺的答案也很好.即,C++ 規范是怎么說的?
澄清:我不是在談論類在 c'tor 中自己分配內存然后拋出的情況.我很感激在這些情況下不會調用 d'tor.我說的是用于分配 THE 對象的內存(在我的例子中是 Blah
).
你應該參考類似的問題 此處 和此處.基本上,如果構造函數拋出異常,您就可以安全地再次釋放對象本身的內存.雖然,如果在構造函數期間已經聲明了其他內存,則在離開構造函數之前,您需要自行釋放它,但有異常.
對于誰刪除內存的問題,答案是 new-operator 背后的代碼(由編譯器生成).如果它識別出離開構造函數的異常,它必須調用類成員的所有析構函數(因為那些已經在調用構造函數代碼之前成功構造)并釋放它們的內存(可以與析構函數調用一起遞歸完成,很可能通過對它們調用適當的delete)以及釋放為此類本身分配的內存.然后它必須將捕獲的異常從構造函數重新拋出給 new 的調用者.當然,可能還有更多工作要做,但我無法從腦海中提取所有細節,因為它們取決于每個編譯器的實現.
I really can't believe I couldn't find a clear answer to this...
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?
}
}
When I tried this out, b
is NULL in the catch block (which makes sense).
When debugging, I noticed that the conrol enters the memory allocation routine BEFORE it hits the constructor.
This on the MSDN website seems to confirm this:
When new is used to allocate memory for a C++ class object, the object's constructor is called after the memory is allocated.
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?
It would also be nice to get a cross platform answer on this. i.e., what does the C++ spec say?
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.
這篇關于誰刪除了在“新建"期間分配的內存?構造函數中有異常的操作?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!