Make valgrind stop immediately after first error

In addition to redirecting both program and Valgrind output to a file (as already suggested), you can use --db-attach=yes flag, which will cause your program to stop in the debugger right at the error.

This has the advantage that in addition to looking at the log your program produced, you can also look at other program state (that you are not logging).


Valgrind 3.14.0 has --exit-on-first-error option:

ERROR-RELATED OPTIONS         top

       These options are used by all tools that can report errors, e.g.
       Memcheck, but not Cachegrind.

   ...

   --exit-on-first-error=<yes|no> [default: no]
       If this option is enabled, Valgrind exits on the first error. A
       nonzero exit value must be defined using --error-exitcode option.
       Useful if you are running regression tests or have some other
       automated test machinery.

This option must be used together with --error-exitcode option, so possible Valgrind invocation can be:

valgrind --exit-on-first-error=yes --error-exitcode=1 ...

If you want it to stop in the console (not in a file), here is a way to do it :

Use the parameter : --gen-suppressions=yes

When you debug it will stops like this :

==949== Thread 2:
==949== Invalid read of size 4
==949==    at 0x7B62DC0: wcslen (wcslen.S:24)
==949==    by 0x7B62D7D: wcsdup (wcsdup.c:29)
==949==    by 0x52D0476: de_strdup(wchar_t*) (de_string.cpp:1442)
==949==    by 0x437629: void de_format<>(c_de_string&, wchar_t*) (de_string.h:368)
==949==    by 0x45F4FB: int db_select_group<>(s_db*, s_pqexec_param*, wchar_t*, wchar_t*, wchar_t*, wchar_t*, int, wchar_t*) (in /corto/goinfre/code2/cortod.repo/bin/x64/Debug/cortod)
==949==    by 0x45EA96: check_oldgeom(c_cartod*) (cartod_funcs.cpp:114)
==949==    by 0x45EBF8: armserv_update_geom(c_cartod*) (cartod_funcs.cpp:149)
==949==    by 0x455EF9: c_cortosrv_thread::on_timeout() (cartod.cpp:163)
==949==    by 0x52FE500: c_de_thread::loop() (de_thread.cpp:35)
==949==    by 0x52FEE97: thread_loop(void*) (de_thread_priv_linux.cpp:85)
==949==    by 0x506E181: start_thread (pthread_create.c:312)
==949==    by 0x7BBA47C: clone (clone.S:111)
==949==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==949== 
==949== 
==949== ---- Print suppression ? --- [Return/N/n/Y/y/C/c] ---- 

Then you can go to the next one continue all etc.

The normal purpose of this parameter is to remove the false positives, by printing suppression that you can add in a file and pass it to valgrind using the parameter : --suppressions=<filename>