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 6, 2017
1 parent 9c17990 commit 60719ce
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
23 changes: 23 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,6 +37,7 @@
#include <set>

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

static bool memleakfinder_initialised = false;
bool memleakfinder_global_enable = false;
Expand All @@ -42,6 +47,10 @@ typedef struct
size_t size;
const char *file;
int line;
#if !defined BOX_RELEASE_BUILD && defined HAVE_EXECINFO_H
void *stack_frames[20];
size_t stack_size;
#endif
} MallocBlockInfo;

typedef struct
Expand All @@ -50,6 +59,10 @@ typedef struct
const char *file;
int line;
bool array;
#if !defined BOX_RELEASE_BUILD && defined HAVE_EXECINFO_H
void *stack_frames[20];
size_t stack_size;
#endif
} ObjectInfo;

namespace
Expand Down Expand Up @@ -544,6 +557,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);
#if !defined BOX_RELEASE_BUILD && defined HAVE_EXECINFO_H
if(file == stdout)
{
DumpStackBacktrace(BOX_CURRENT_FILE, i->second.stack_size,
i->second.stack_frames);
}
#endif
}
}
}
Expand Down Expand Up @@ -628,6 +648,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 HAVE_EXECINFO_H
i.stack_size = backtrace(i.stack_frames, 20);
#endif
sObjectBlocks[block] = i;

if(sTrackObjectsInSection)
Expand Down
5 changes: 4 additions & 1 deletion lib/common/Utils.cpp
Expand Up @@ -245,6 +245,9 @@ void DumpStackBacktrace(const std::string& filename, size_t size, void * const *
pInfo->SizeOfStruct = sizeof(SYMBOL_INFO);
#endif

void DumpStackBacktrace(size_t size, void * const * array)
{
#ifdef HAVE_EXECINFO_H
for(size_t i = 0; i < size; i++)
{
std::ostringstream output;
Expand Down Expand Up @@ -298,7 +301,7 @@ void DumpStackBacktrace(const std::string& filename, size_t size, void * const *
}
}


#include "MemLeakFindOn.h"

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

0 comments on commit 60719ce

Please sign in to comment.