Normally it would be destructed upon the scope ending.. I could see issues occurring if exceptions were thrown though.
Yes any scope bound variables will be destroyed.
void work()
{
Foo a;
Foo* b = new Foo;
// ... later
// exception thrown
delete b;
}
In this example a
's destructor would be called when the exception was thrown as the stack unwound, but the memory pointed to by b
would be leaked since it would never reach the delete
call. This is one of the many reasons why RAII is so helpful.