https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117442
--- Comment #2 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by David Malcolm <dmalc...@gcc.gnu.org>: https://gcc.gnu.org/g:8c4184682d1cdfc43296f9550a48eaadb7501bbd commit r15-4969-g8c4184682d1cdfc43296f9550a48eaadb7501bbd Author: David Malcolm <dmalc...@redhat.com> Date: Tue Nov 5 18:30:39 2024 -0500 fortran: dynamically allocate error_buffer [PR117442] PR fortran/117442 reports a crash on exit of f951 when configured with --enable-gather-detailed-mem-stats. The crash happens if any diagnostics were ever buffered into error_buffer. The root cause is that error_buffer is statically allocated and thus has a non-trivial destructor called at exit. If error_buffer's diagnostic_buffer ever buffered anything, then a diagnostic_per_format_buffer will have been created for the buffer per-output-sink, and the destructors for these call into the mem-stats subsystem, which has already beeen cleaned up. The simplest fix is to allocate error_buffer on the heap, rather that statically, which fixes the crash. There's a comment about error_buffer: /* pp_error_buffer is statically allocated. This simplifies memory management when using gfc_push/pop_error. */ added by Manu in r6-1748-g5862c189c2c3c2 while fixing PR fortran/66528. The comment appears to be out of date. I've tested maxerrors.f90 under valgrind, and it's clean with the patch. gcc/fortran/ChangeLog: PR fortran/117442 * error.cc (error_buffer): Convert to a pointer so it can be heap-allocated. (gfc_error_now): Update for error_buffer being heap-allocated. (gfc_clear_error): Likewise. (gfc_error_flag_test): Likewise. (gfc_error_check): Likewise. (gfc_push_error): Likewise. (gfc_pop_error): Likewise. (gfc_diagnostics_init): Allocate error_buffer on the heap, rather than statically. (gfc_diagnostics_finish): Delete error_buffer. Signed-off-by: David Malcolm <dmalc...@redhat.com>