Qt segmentation fault unless I rebuild
I'm working on a Qt project and often when I make a bunch of changes and build, when I run the program I get a random segmentation fault somewhere in the Qt framework.
To fix it I have to rebuild and then it goes away.
I found another person who had this issue and they received this answer:
from: Segmentation fault in Qt application framework
This makes it sound as though your build system isn't recognizing a dependency and that a change to that class definition isn't triggering a rebuild of something that should be recompiled when the definition changes.
Make sure class LevelIndicator is defined in exactly one place (generally that would be a header file that gets included by whatever modules need to use a LevelIndicator object). Also make sure that any global/static instances of LevelIndicator objects are following the one definition rule.
So that makes sense to me, h开发者_高级运维owever each class is defined in only one place, with #ifndef guards to prevent double inclusions. So how can I avoid this?
Usually such errors happen if you change header files but some source file isn't rebuilt, e.g. by adding members to structs/classes. QMake's dependency handling is peculiar in that regard. If you include headers from other directories using the INCLUDEPATH variable, you must also add the directory to the DEPENDPATH variable to have them "monitored" for changes. Otherwise changes in the INCLUDEPATH directories won't trigger rebuilds in the current directory. It should look like this:
INCLUDEPATH += ../somelib/include
DEPENDPATH += ../somelib/include
Rule of thumb: If you see a INCLUDEPATH directive pointing to a path inside your project without corresponding DEPENDPATH directive, almost always that's wrong.
See also my answer to a similar question.
精彩评论