> > In the program below, why am I not seeing operator delete being invoked?
Because execution goes straight to std::terminate.
Catch that exception somewhere.
Here's the program I'm now running per your suggestion:
#include <iostream>
using namespace std;
class foo
{
public:
foo() { throw 1; }
static void *operator new(size_t s)
{
cout << "foo::operator new()" << endl;
return ::operator new(s);
}
static void operator delete(void *p)
{
cout << "foo::operator delete()" << endl;
::operator delete(p);
}
};
int main()
{
try
{
new foo;
}
catch(...)
{
cout << "Exception handler" << endl;
}
}
OK, catching the exception in main() did allow foo::operator delete() to be
called. Here's the output:
foo::operator new()
foo::operator delete()
Exception handler
So, operator delete is called immediately after operator new().
Specifically, it is called before the exception is propagated on. Now my
question is: Why did I have to catch the exception in order for operator
delete to be called given that the call to operator delete() occurs before
any exception processing anyway? What is the real sequence of events going
on here, both with and without the exception handler?
Thanks!