boostorg / stacktrace

C++ library for storing and printing backtraces.
https://boost.org/libs/stacktrace
422 stars 70 forks source link

Memory leak due to calling `backtrace_create_state` multiple times #118

Closed Cogitri closed 6 months ago

Cogitri commented 2 years ago

Hello,

currently boost-stacktrace with the backtrace backend will call backtrace_create_state once for each thread. This creates a memory leak since each time a new thread is created, a new backtrace_state is allocated but it's never freed (and can't be since there's no backtrace_destroy_state). See e.g. https://patchwork.ozlabs.org/project/gcc/patch/3defaafeedc9fa6b42e61401d4c8c84e@starynkevitch.net/#1625428 for more discussion on this.

apolukhin commented 2 years ago

Unfortunately, it's not a bug but a feature:

https://github.com/boostorg/stacktrace/blob/08d720adbd01c0187af256eb491cffece7e87857/include/boost/stacktrace/detail/libbacktrace_impls.hpp#L74-L85

There's some issue either with the packaged versions of libbacktrace, or some issue in the libbacktrace with some compiler flags that I failed to localize https://gcc.gnu.org/bugzilla//show_bug.cgi?id=87653

I have to add more docs and references into the sources and provide a macro to force single instance for those' who wish to take the risk

pergraa-celonis commented 1 year ago

Should that be static and not ststic here? https://github.com/boostorg/stacktrace/commit/308b7f6b087a302795d6a189373f254f610a826f#diff-1f3dccb3928ad7b52e4c986270cb619b663197283b764c2fb68256d285f21a3fR124

apolukhin commented 1 year ago

Oops, thanks!