Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak due to calling backtrace_create_state multiple times #118

Closed
Cogitri opened this issue Jan 12, 2022 · 3 comments
Closed

Memory leak due to calling backtrace_create_state multiple times #118

Cogitri opened this issue Jan 12, 2022 · 3 comments

Comments

@Cogitri
Copy link

Cogitri commented Jan 12, 2022

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
Copy link
Member

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

// TODO: The most obvious solution:
//
//static ::backtrace_state* state = ::backtrace_create_state(
// prog_location.name(),
// 1, // allow safe concurrent usage of the same state
// boost::stacktrace::detail::libbacktrace_error_callback,
// 0 // pointer to data that will be passed to callback
//);
//
//
// Unfortunately, that solution segfaults when `construct_state()` function is in .so file
// and multiple threads concurrently work with state.

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

apolukhin added a commit that referenced this issue Sep 2, 2022
yiguolei pushed a commit to apache/doris that referenced this issue Nov 15, 2022
boost::stacktrace::stacktrace() has memory leak, so use glog internal func to print stacktrace.
The reason for the memory leak of boost::stacktrace is that a state is saved in the thread local of each thread but not actively released. The test found that each thread leaked about 100M after calling boost::stacktrace.
refer to:
boostorg/stacktrace#118
boostorg/stacktrace#111
@pergraa-celonis
Copy link

Should that be static and not ststic here?
308b7f6#diff-1f3dccb3928ad7b52e4c986270cb619b663197283b764c2fb68256d285f21a3fR124

@apolukhin
Copy link
Member

Oops, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants