C++ segfault occurs after main() returns

A junior developer I work with came to me recently with an interesting issue.  A simple C++ application he had written was seg faulting and he couldn't figure out why.  If you are thinking this sounds like a threading issue thn you are wrong this time.  I have come across many seg faults occuring because threads aren't cleaned up properly.  However, in this case the applicationwas single threaded.  With a little bit of digging it became clear that the issue was caused by a singleton object he was using.  This object held a pointer to another object and when the destructor was called it attempted to clean up an object it held a pointer to internally.  Unfortunately because this object was a static in the global scope the iner object was cleaned up before the outer dereferenced a dangling pointer.

Comments

Popular Posts