Search code examples
crashqt-creatorvalgrindcrash-dumps

Program crashes valgrind only in Qt Creator


I'm writing a C++ program with several threads, lots of IO, etc. and I want to check for memory leaks. The program runs great in debug build, and release build. But, if I run Valgrind from within Qt Creator (v 12.0.2) with Qt5.15.2, I see some output from my program and then:

21:18:04: Analyzing finished.
21:18:04: Process exited with return value Process crashed
21:18:04: Analyzing finished.

But there is no output showing where it crashed! I tried running valgrind on my program from the bash command prompt and it runs fine (though LOTS of output because of no supression files for Qt). No crash.

There's something about running Valgrind in Qt Creator that exposes a memory problem (I'm guessing). But I can't find it! I have core (crash) files enabled, yet my program crashing does not cause a core dump to be written. I'm at a loss...how do I find out what's going on to cause the crash?


Solution

  • I discovered the cause (and it extends a solution I discovered long ago). Qt can use self modifying code (SMC) when performing QRegularExpression or QRegEx operations.

    SMC can crash valgrind, which is why I always enable the smc-check=all option in Projects>Run>Valgrind Settings>Valgrind Generic Settings. If you expand that box there is an option for WHERE to check for SMC, which by default is only on stack.

    It turns out I started to make my QRegularExpressions static (recommended by Qt since they are resource intensive), which moved them to the heap. Once I changed the above option to "Everywhere" valgrind was happy again.

    I'm not sure what the command line equivalent is, but outside of Qt Creator valgrind ran fine with only the --smc-check=all option. But inside Qt Creator the SMC seemed to steer pointers into problem areas that caused valgrind to crash.