user500944
user500944

Reputation:

Weird Segfaults Go Away After Full Rebuild

More than one time I have encountered the following problem when building code with g++:

Everything seems fine. You compile, run and then boom - a segfault jumps at you out of nowhere. You start looking at the code, can't find anything, do make clean && make and the problem goes away.

I have seen this happen with different g++ version, different machines, different kernels.

Why does this happen? Is there a way to prevent it?

Upvotes: 0

Views: 195

Answers (1)

DataPlusPlus
DataPlusPlus

Reputation: 1120

If you change the header files, then there is no mechanism in the Makefile to figure out which source files include these, and need to be recompiled. A problem you might be having is if header files A.h defines a class A, and you add or remove or rearrange members of A, the size of the class changes. The old size is still being used in some of the source files, however, and so you might end up with one of your source files over-allocating or under-allocating when you create an instance of your class, on the stack or on the heap. make clean forces the new information about the class to propogate as all the source files have to recompile.

If you make significant changes to the header files, it's probably safest to just do a full rebuild.

Upvotes: 2

Related Questions