Nvwa  1.1
Classes | Namespaces | Macros | Typedefs | Functions | Variables
debug_new.h File Reference

Header file for checking leaks caused by unmatched new/delete. More...

#include <new>
#include <stdio.h>
#include "_nvwa.h"
#include "c++11.h"
Include dependency graph for debug_new.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  nvwa::debug_new_recorder
 Recorder class to remember the call context. More...
 
class  nvwa::debug_new_counter
 Counter class for on-exit leakage check. More...
 

Namespaces

 nvwa
 Namespace of the nvwa project.
 

Macros

#define _DEBUG_NEW_TYPE   1
 Macro to indicate which variant of DEBUG_NEW is wanted. More...
 
#define DEBUG_NEW   NVWA::debug_new_recorder(__FILE__, __LINE__) ->* new
 Macro to catch file/line information on allocation. More...
 

Typedefs

typedef void(* nvwa::stacktrace_print_callback_t) (FILE *fp, void **stacktrace)
 Callback type for stack trace printing. More...
 
typedef bool(* nvwa::leak_whitelist_callback_t) (char const *file, int line, void *addr, void **stacktrace)
 Callback type for the leak whitelist function. More...
 

Functions

void * operator new (size_t size, const char *file, int line)
 Allocates memory with file/line information. More...
 
void * operator new[] (size_t size, const char *file, int line)
 Allocates array memory with file/line information. More...
 
void operator delete (void *ptr, const char *file, int line) noexcept
 Placement deallocation function. More...
 
void operator delete[] (void *ptr, const char *file, int line) noexcept
 Placement deallocation function. More...
 
int nvwa::check_leaks ()
 Checks for memory leaks. More...
 
int nvwa::check_mem_corruption ()
 Checks for heap corruption. More...
 

Variables

bool nvwa::new_autocheck_flag = true
 Flag to control whether nvwa::check_leaks will be automatically called on program exit.
 
bool nvwa::new_verbose_flag = false
 Flag to control whether verbose messages are output.
 
FILE * nvwa::new_output_fp = stderr
 Pointer to the output stream. More...
 
const char * nvwa::new_progname = _DEBUG_NEW_PROGNAME
 Pointer to the program name. More...
 
stacktrace_print_callback_t nvwa::stacktrace_print_callback = nullptr
 Pointer to the callback used to print the stack backtrace in case of a memory problem. More...
 
leak_whitelist_callback_t nvwa::leak_whitelist_callback = nullptr
 Pointer to the callback used to filter out false positives from leak reports. More...
 
static debug_new_counter nvwa::__debug_new_count
 Counting object for each file including debug_new.h. More...
 

Detailed Description

Header file for checking leaks caused by unmatched new/delete.

Date
2015-10-25

Macro Definition Documentation

◆ _DEBUG_NEW_TYPE

#define _DEBUG_NEW_TYPE   1

Macro to indicate which variant of DEBUG_NEW is wanted.

The default value 1 allows the use of placement new (like new(std::nothrow)), but the verbose output (when nvwa::new_verbose_flag is true) looks worse than some older versions (no file/line information for allocations). Define it to 2 to revert to the old behaviour that records file and line information directly on the call to operator new.

◆ DEBUG_NEW

#define DEBUG_NEW   NVWA::debug_new_recorder(__FILE__, __LINE__) ->* new

Macro to catch file/line information on allocation.

If _DEBUG_NEW_REDEFINE_NEW is 0, one can use this macro directly; otherwise new will be defined to it, and one must use new instead.

Function Documentation

◆ operator delete()

void operator delete ( void *  ptr,
const char *  file,
int  line 
)
noexcept

Placement deallocation function.

For details, please check Section 5.3.4 of the C++ 1998 or 2011 Standard.

Parameters
ptrpointer to the previously allocated memory
filenull-terminated string of the file name
lineline number
See also
http://www.csci.csusb.edu/dick/c++std/cd2/expr.html#expr.new
http://wyw.dcweb.cn/leakage.htm

◆ operator delete[]()

void operator delete[] ( void *  ptr,
const char *  file,
int  line 
)
noexcept

Placement deallocation function.

For details, please check Section 5.3.4 of the C++ 1998 or 2011 Standard.

Parameters
ptrpointer to the previously allocated memory
filenull-terminated string of the file name
lineline number

◆ operator new()

void* operator new ( size_t  size,
const char *  file,
int  line 
)

Allocates memory with file/line information.

Parameters
sizesize of the required memory block
filenull-terminated string of the file name
lineline number
Returns
pointer to the memory allocated; or null if memory is insufficient (_DEBUG_NEW_STD_OPER_NEW is 0)
Exceptions
bad_allocmemory is insufficient (_DEBUG_NEW_STD_OPER_NEW is 1)

◆ operator new[]()

void* operator new[] ( size_t  size,
const char *  file,
int  line 
)

Allocates array memory with file/line information.

Parameters
sizesize of the required memory block
filenull-terminated string of the file name
lineline number
Returns
pointer to the memory allocated; or null if memory is insufficient (_DEBUG_NEW_STD_OPER_NEW is 0)
Exceptions
bad_allocmemory is insufficient (_DEBUG_NEW_STD_OPER_NEW is 1)