typedef struct Object {
// some variable declarations.
Object(): var_(var) {}
~Object();
private:
// Prevent the compiler from generating a default
// copy constructor and assignment operator
DISABLE_COPY_AND_ASSIGN( Object );
} Object;
Object::~Object() {
assert( !"Destructor called" );.
if ( 1 ) {
logDebug( MODULE_STR_ID, "hello" );
assert( isUnitTest );
unlink( someFile );
}
}
static Object obj;
// main() is in some other file.
I expect the assert
in the destructor to be hit but it doesn't. In the accepted answer of Does C++ call destructors for global and class static variables?, it says that the compiler can optimize out the destructor if the observable behavior is the same even without the destructor invocation. But I don't see how that is the case with my example. I even verified that the destructor is not compiled out by printing the instructions out in GDB. If I set a breakpoint in the destructor, GDB hits an internal error at the end of the program!
PS: If I run just the above code ( of course, making it compilable ) in an online IDE, it works just fine. Is something wrong with my environment?
EDIT: The above sample compiled as a standalone program just works fine. Just doesn't inside my big project. So I really cannot provide more code to replicate the problem, as it happens only in my project. And with or without unlink
, the problem is the same. I'm really looking for ideas about what might be going wrong in my real project( environment etc ) than make the above code work as a standalone program. And it is a google test that is failing at an EXPECT_EQ