Skip to content

Commit

Permalink
Store and log full stack traces for leaked memory allocations
Browse files Browse the repository at this point in the history
Don't dump stack backtrace when logging memory leaks to a file

Since the backtrace goes to the console instead, it's confusing to have them in
different places.

(cherry picked from commit 6e7df4d)
(cherry picked from commit 270cd1f)
(cherry picked from commit c627e95)
  • Loading branch information
qris committed Jul 23, 2017
1 parent 04ece62 commit 04ec616
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
27 changes: 27 additions & 0 deletions lib/common/DebugMemLeakFinder.cpp
Expand Up @@ -20,6 +20,10 @@
#include <stdio.h>
#include <string.h>

#ifdef HAVE_EXECINFO_H
# include <execinfo.h>
#endif

#ifdef HAVE_PROCESS_H
# include <process.h>
#endif
Expand All @@ -33,15 +37,24 @@
#include <set>

#include "MemLeakFinder.h"
#include "Utils.h"

static bool memleakfinder_initialised = false;
bool memleakfinder_global_enable = false;

#if !defined BOX_RELEASE_BUILD && defined HAVE_EXECINFO_H
# define BOX_MEMORY_LEAK_BACKTRACE_ENABLED
#endif

typedef struct
{
size_t size;
const char *file;
int line;
#ifdef BOX_MEMORY_LEAK_BACKTRACE_ENABLED
void *stack_frames[20];
size_t stack_size;
#endif
} MallocBlockInfo;

typedef struct
Expand All @@ -50,6 +63,10 @@ typedef struct
const char *file;
int line;
bool array;
#ifdef BOX_MEMORY_LEAK_BACKTRACE_ENABLED
void *stack_frames[20];
size_t stack_size;
#endif
} ObjectInfo;

namespace
Expand Down Expand Up @@ -544,6 +561,13 @@ void memleakfinder_reportleaks_file(FILE *file)
"%s:%d\n", i->second.array?" []":"",
i->first, (unsigned long)i->second.size, i->second.file,
i->second.line);
#ifdef BOX_MEMORY_LEAK_BACKTRACE_ENABLED
if(file == stdout)
{
DumpStackBacktrace(__FILE__, i->second.stack_size,
i->second.stack_frames);
}
#endif
}
}
}
Expand Down Expand Up @@ -628,6 +652,9 @@ void add_object_block(void *block, size_t size, const char *file, int line, bool
i.file = file;
i.line = line;
i.array = array;
#ifdef BOX_MEMORY_LEAK_BACKTRACE_ENABLED
i.stack_size = backtrace(i.stack_frames, 20);
#endif
sObjectBlocks[block] = i;

if(sTrackObjectsInSection)
Expand Down
15 changes: 11 additions & 4 deletions lib/common/Utils.cpp
Expand Up @@ -290,15 +290,22 @@ void DumpStackBacktrace(const std::string& filename, size_t size, void * const *
(void *)diff;
}

// Instead of calling BOX_TRACE, we call Logging::Log directly in order to pass filename
// as the source file. This allows exception backtraces to be turned on and off by file,
// instead of all of them originating in Utils.cpp.
// Instead of calling BOX_TRACE, we call Logging::Log directly in order to pass
// filename as the source file. This allows exception backtraces to be turned on
// and off by file, instead of all of them originating in Utils.cpp.
Logging::Log(Log::TRACE, filename, 0, // line
__FUNCTION__, BACKTRACE, output.str());

// We don't really care about frames after main() (e.g. CRT startup), and sometimes
// they contain invalid data (not function pointers) such as 0x7.
if(mangled_name != NULL && strcmp(mangled_name, "main") == 0)
{
break;
}
}
}


#include "MemLeakFindOn.h"

// --------------------------------------------------------------------------
//
Expand Down

0 comments on commit 04ec616

Please sign in to comment.