From efc134dba34baa942fd698e842ee2c6a8f57252e Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Thu, 3 Sep 2015 15:01:59 -0700 Subject: [PATCH] Only close stdin/stdout/stderr for debug malloc. The debug malloc code unconditionally closes stdin/stdout/stderr, which means that other atexit functions cannot use them. Only close these if there is a debug malloc final function to call. This doesn't appear to be a problem on most normal applications or the atexit_exit bionic unit test would be failing. However, if you enable stat dumping in jemalloc, nothing prints. Most likely trying to add an atexit function from within libc is causing that atexit to run after the debug malloc atexit function. Change-Id: I963720d4ccaaa511e44af07a7461f17eb3f84e8e --- libc/bionic/malloc_debug_common.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/libc/bionic/malloc_debug_common.cpp b/libc/bionic/malloc_debug_common.cpp index ee796c6dc..5b7c42caa 100644 --- a/libc/bionic/malloc_debug_common.cpp +++ b/libc/bionic/malloc_debug_common.cpp @@ -486,18 +486,19 @@ static void malloc_init_impl() { } static void malloc_fini_impl() { - // Our BSD stdio implementation doesn't close the standard streams, it only flushes them. - // And it doesn't do that until its atexit handler is run, and we run first! - // It's great that other unclosed FILE*s show up as malloc leaks, but we need to manually - // clean up the standard streams ourselves. - fclose(stdin); - fclose(stdout); - fclose(stderr); - if (libc_malloc_impl_handle != NULL) { MallocDebugFini malloc_debug_finalize = reinterpret_cast(dlsym(libc_malloc_impl_handle, "malloc_debug_finalize")); if (malloc_debug_finalize != NULL) { + // Our BSD stdio implementation doesn't close the standard streams, + // it only flushes them. And it doesn't do that until its atexit + // handler is run, and we run first! It's great that other unclosed + // FILE*s show up as malloc leaks, but we need to manually clean up + // the standard streams ourselves. + fclose(stdin); + fclose(stdout); + fclose(stderr); + malloc_debug_finalize(g_malloc_debug_level); } }