I read that once an exception has been thrown, an object that goes out of scope will be destroyed. So I wrote a code to test it.
#include "stdafx.h"
#include <iostream>
using namespace std;
class E {
public:
int v = 0;
};
void f() {
E e;
E *pointer = &e;
e.v = 7;
throw pointer;
}
int main(void) {
E* MainPointer = new E;
try {
f();
}
catch (E* e) {
cout << e -> v; //was executed
MainPointer = e;
}
cout << MainPointer->v; //was executed
system("pause");
return 0;
}
The out put was 77, meaning that both the catch block and the final cout was executed. However I was expecting a memory error at the final cout as the object pointed to by MainPointer should have been deallocated by now.
Can someone clarify why the object declared in f() was not deallocated.