S
spipyeah
In the C++ FAQ Lite, it is said in question 16.9:
If an exception occurs during the Fred constructor of p = new Fred(),
the C++ language guarantees that the memory sizeof(Fred) bytes that
were allocated will automagically be released back to the heap.
Naturally, any locally declared objects in the class are destroyed as
the stack is unwound. But what about objects allocated in the
constructor using new?
I totally expect the answer to be that you have to delete them
yourself. I just want to be 100% sure.
So given class Fred, the following would be necessary in Fred's
constructor, right?
class Fred
{
public:
Fred();
Joe *p;
}
Fred::Fred()
{
try
{
joe = new Joe();
// do some more stuff
}
catch( ... )
{
// must manually delete joe
delete joe;
throw;
}
}
Thank you.
If an exception occurs during the Fred constructor of p = new Fred(),
the C++ language guarantees that the memory sizeof(Fred) bytes that
were allocated will automagically be released back to the heap.
Naturally, any locally declared objects in the class are destroyed as
the stack is unwound. But what about objects allocated in the
constructor using new?
I totally expect the answer to be that you have to delete them
yourself. I just want to be 100% sure.
So given class Fred, the following would be necessary in Fred's
constructor, right?
class Fred
{
public:
Fred();
Joe *p;
}
Fred::Fred()
{
try
{
joe = new Joe();
// do some more stuff
}
catch( ... )
{
// must manually delete joe
delete joe;
throw;
}
}
Thank you.