I made a mistake that could have cost my application its stability. Learn from the error of my ways!


Cleaning up some code, I saw an opportunity for a forward declaration and I took it:

struct T2;

struct T1
{
    std::auto_ptr obj;
};

Only by chance, some hours later, did I spot a diagnostic note from GCC hidden away in my build output:

note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.

Deleting an object of an incomplete type yields undefined behaviour, and since obviously the standard library runtime implementing std::auto_ptr does not define my type T2, the destruction of an std::auto_ptr now invokes this UB.

I was lucky to spot this — I figure GCC should emit a warning rather than a note, since I'm trained to spot them from a mile away. In the meantime, that's one mystical shutdown-time segfault (that I'd shelved for later investigation, not considering that it could be linked to this code change) explained.

#include "T2.h"

struct T1
{
    std::auto_ptr obj;
};

I lose my forward declaration, but at least my program is well-defined and will not open black holes. So there are kind of some pluses and minuses.