Why this code not call CloseHandles in class destructor?
In my code test I call '((MyClass*)pThis)->CloseHandles();' explicitly, but variable m_bFinished have wrong value. Why ?
#include <windows.h>
#include <exception>
class MyClass
{
public:
explicit MyClass( void **pThis)
{
*pThis = this;
m_bFinished = false;
//code open handle here
//an error occurs
throw new std::exception("Exception thrown!");
}
~MyClass()
{
if ( ! m_bFinished ) CloseHandles();
}
void CloseHandles()
{
if ( m_bFinished ) return;
//close handles here.
m_bFinished = true;
}
private:
bool m_bFinished;
};
int main(int argc, char* argv[])
{
MyClass * pMyClass;
void * pThis = NULL;
try
{
pMyClass = new MyClass(&pThis);
}
catch(std::exception * e)
{
//delete pThis;
if ( pThis )
{
((MyClass*)pThis)->CloseHandles();
}
}
return 0;
}