-1
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

Community
  • 1
  • 1
Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
  • In practical, your program must call a function in the source file which contains that global object. Otherwise, the linker won't link that object file. – Danh Jan 20 '17 at 05:58
  • Or, with gcc, you need to pass `-Wl,--whole-archive` options. See http://stackoverflow.com/questions/14116420/how-to-force-gcc-to-link-an-unused-static-library – Danh Jan 20 '17 at 05:58
  • We really do need enough code to replicate the problem. For example, what is `someFile`? – David Schwartz Jan 20 '17 at 06:20
  • @DavidSchwartz `someFile` is just an ordinary text file opened in RD/WR mode elsewhere. @Danh There are huge number of functions in the same file that are all getting called. – Pavan Manjunath Jan 20 '17 at 06:48
  • @PavanManjunath Is it a global though? It may already be destroyed when this destructor runs. We need to see the code. There's lots of complex ways you could get this wrong. – David Schwartz Jan 20 '17 at 12:08

2 Answers2

1

I was running the above program as part of a Google test that exits main by a call to _exit(). And it seems that destructors for static global objects aren't called when a program is terminated by a call to _exit(). See this answer and this

Community
  • 1
  • 1
Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
0

The destructor will always be called in your code.

If I set a breakpoint in the destructor, GDB hits an internal error at the end of the program!

Try not to call unlink( someFile ) in your destructor of a global object, because the main function has exited and maybe some related library has been uninitialized at this moment, the unlink call may cause some unexpected behaviors. It should be why GDB hits an internal error.

Why did you say that your destructor is not compiled out? Please try to verify it by disassemblying your code with objdump and addr2line.

Jun Ge
  • 408
  • 4
  • 13
  • `unlink` or not, the behavior seems to be the same. I did `x/i StasisHaState::~StasisHaState()` and the function's contents are there – Pavan Manjunath Jan 20 '17 at 07:00
  • Maybe you should find problems in other code of your project. If your project isn't big, there's a time cost but simple method to check it. You can remove all the code from your project and only leave above sample code, then add code file by file, finally you can find which part cause the problem. – Jun Ge Jan 20 '17 at 07:18