Implementation of debug versions of new and delete to check leakage. More...
#include <new>#include <assert.h>#include <limits.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include "fast_mutex.h"#include "static_assert.h"#include "debug_new.h"
Classes | |
| struct | new_ptr_list_t |
Structure to store the position information where new occurs. More... | |
Defines | |
| #define | _DEBUG_NEW_ALIGNMENT 16 |
| The alignment requirement of allocated memory blocks. | |
| #define | _DEBUG_NEW_CALLER_ADDRESS __builtin_return_address(0) |
| The expression to return the caller address. | |
| #define | _DEBUG_NEW_ERROR_ACTION abort() |
| The action to take when an error occurs. | |
| #define | _DEBUG_NEW_FILENAME_LEN 44 |
| The length of file name stored if greater than zero. | |
| #define | _DEBUG_NEW_PROGNAME NULL |
| The program (executable) name to be set at compile time. | |
| #define | _DEBUG_NEW_STD_OPER_NEW 1 |
Macro to indicate whether the standard-conformant behaviour of operator new is wanted. | |
| #define | _DEBUG_NEW_TAILCHECK 0 |
| Macro to indicate whether a writing-past-end check will be performed. | |
| #define | _DEBUG_NEW_TAILCHECK_CHAR 0xCC |
| Value of the padding bytes at the end of a memory block. | |
| #define | _DEBUG_NEW_USE_ADDR2LINE 1 |
| Whether to use addr2line to convert a caller address to file/line information. | |
| #define | _DEBUG_NEW_REDEFINE_NEW 0 |
Macro to indicate whether redefinition of new is wanted. | |
| #define | ALIGN(s) (((s) + _DEBUG_NEW_ALIGNMENT - 1) & ~(_DEBUG_NEW_ALIGNMENT - 1)) |
| Gets the aligned value of memory block size. | |
Functions | |
| static bool | print_position_from_addr (const void *addr) |
| Tries printing the position information from an instruction address. | |
| static void | print_position (const void *ptr, int line) |
| Prints the position information of a memory operation point. | |
| static void * | alloc_mem (size_t size, const char *file, int line, bool is_array) |
| Allocates memory and initializes control data. | |
| static void | free_pointer (void *pointer, void *addr, bool is_array) |
| Frees memory and adjusts pointers. | |
| int | check_leaks () |
| Checks for memory leaks. | |
| int | check_mem_corruption () |
| Checks for heap corruption. | |
| void * | operator new (size_t size, const char *file, int line) |
| Allocates memory with file/line information. | |
| void * | operator new[] (size_t size, const char *file, int line) |
| Allocates array memory with file/line information. | |
| void * | operator new (size_t size) throw (std::bad_alloc) |
| Allocates memory without file/line information. | |
| void * | operator new[] (size_t size) throw (std::bad_alloc) |
| Allocates array memory without file/line information. | |
| void * | operator new (size_t size, const std::nothrow_t &) throw () |
| Allocates memory with no-throw guarantee. | |
| void * | operator new[] (size_t size, const std::nothrow_t &) throw () |
| Allocates array memory with no-throw guarantee. | |
| void | operator delete (void *pointer) throw () |
| Deallocates memory. | |
| void | operator delete[] (void *pointer) throw () |
| Deallocates array memory. | |
| void | operator delete (void *pointer, const char *file, int line) throw () |
| Placement deallocation function. | |
| void | operator delete[] (void *pointer, const char *file, int line) throw () |
| Placement deallocation function. | |
| void | operator delete (void *pointer, const std::nothrow_t &) throw () |
| Placement deallocation function. | |
| void | operator delete[] (void *pointer, const std::nothrow_t &) throw () |
| Placement deallocation function. | |
Variables | |
| const size_t | PLATFORM_MEM_ALIGNMENT = sizeof(size_t) * 2 |
| The platform memory alignment. | |
| const unsigned | MAGIC = 0x4442474E |
| Definition of the constant magic number used for error detection. | |
| const int | ALIGNED_LIST_ITEM_SIZE = ALIGN(sizeof(new_ptr_list_t)) |
The extra memory allocated by operator new. | |
| static new_ptr_list_t | new_ptr_list |
| List of all new'd pointers. | |
| static fast_mutex | new_ptr_lock |
| The mutex guard to protect simultaneous access to the pointer list. | |
| static fast_mutex | new_output_lock |
| The mutex guard to protect simultaneous output to new_output_fp. | |
| static size_t | total_mem_alloc = 0 |
| Total memory allocated in bytes. | |
| bool | new_autocheck_flag = true |
| Flag to control whether check_leaks will be automatically called on program exit. | |
| bool | new_verbose_flag = false |
| Flag to control whether verbose messages are output. | |
| FILE * | new_output_fp = stderr |
| Pointer to the output stream. | |
| const char * | new_progname = _DEBUG_NEW_PROGNAME |
| Pointer to the program name. | |
Implementation of debug versions of new and delete to check leakage.
| #define _DEBUG_NEW_ALIGNMENT 16 |
The alignment requirement of allocated memory blocks.
It must be a power of two.
| #define _DEBUG_NEW_CALLER_ADDRESS __builtin_return_address(0) |
The expression to return the caller address.
print_position will later on use this address to print the position information of memory operation points.
| #define _DEBUG_NEW_ERROR_ACTION abort() |
The action to take when an error occurs.
The default behaviour is to call abort, unless _DEBUG_NEW_ERROR_CRASH is defined, in which case a segmentation fault will be triggered instead (which can be useful on platforms like Windows that do not generate a core dump when abort is called).
| #define _DEBUG_NEW_FILENAME_LEN 44 |
The length of file name stored if greater than zero.
If it is zero, only a const char pointer will be stored. Currently the default value is non-zero (thus to copy the file name) on non-Windows platforms, because I once found that the exit leakage check could not access the address of the file name on Linux (in my case, a core dump occurred when check_leaks tried to access the file name in a shared library after a SIGINT). This value makes the size of new_ptr_list_t 64 on non-Windows 32-bit platforms.
| #define _DEBUG_NEW_PROGNAME NULL |
The program (executable) name to be set at compile time.
It is better to assign the full program path to new_progname in main (at run time) than to use this (compile-time) macro, but this macro serves well as a quick hack. Note also that double quotation marks need to be used around the program name, i.e., one should specify a command-line option like -D_DEBUG_NEW_PROGNAME="a.out" in bash, or -D_DEBUG_NEW_PROGNAME="a.exe" in the Windows command prompt.
| #define _DEBUG_NEW_REDEFINE_NEW 0 |
Macro to indicate whether redefinition of new is wanted.
Here it is defined to 0 to disable the redefinition of new.
| #define _DEBUG_NEW_STD_OPER_NEW 1 |
Macro to indicate whether the standard-conformant behaviour of operator new is wanted.
It is on by default now, but the user may set it to 0 to revert to the old behaviour.
| #define _DEBUG_NEW_TAILCHECK 0 |
Macro to indicate whether a writing-past-end check will be performed.
Define it to a positive integer as the number of padding bytes at the end of a memory block for checking.
| #define _DEBUG_NEW_TAILCHECK_CHAR 0xCC |
Value of the padding bytes at the end of a memory block.
| #define _DEBUG_NEW_USE_ADDR2LINE 1 |
Whether to use addr2line to convert a caller address to file/line information.
Defining it to a non-zero value will enable the conversion (automatically done if GCC is detected). Defining it to zero will disable the conversion.
| #define ALIGN | ( | s | ) | (((s) + _DEBUG_NEW_ALIGNMENT - 1) & ~(_DEBUG_NEW_ALIGNMENT - 1)) |
Gets the aligned value of memory block size.
| static void* alloc_mem | ( | size_t | size, | |
| const char * | file, | |||
| int | line, | |||
| bool | is_array | |||
| ) | [static] |
Allocates memory and initializes control data.
| size | size of the required memory block | |
| file | null-terminated string of the file name | |
| line | line number | |
| is_array | boolean value whether this is an array operation |
NULL if memory allocation is not successful | int check_leaks | ( | ) |
Checks for memory leaks.
| int check_mem_corruption | ( | ) |
Checks for heap corruption.
| static void free_pointer | ( | void * | pointer, | |
| void * | addr, | |||
| bool | is_array | |||
| ) | [static] |
Frees memory and adjusts pointers.
| pointer | pointer to the previously allocated memory | |
| addr | pointer to the caller | |
| is_array | flag indicating whether it is invoked by a delete[] call |
| void operator delete | ( | void * | pointer, | |
| const std::nothrow_t & | ||||
| ) | throw () |
Placement deallocation function.
For details, please check Section 5.3.4 of the C++ 1998 Standard.
| pointer | pointer to the previously allocated memory |
| void operator delete | ( | void * | pointer, | |
| const char * | file, | |||
| int | line | |||
| ) | throw () |
Placement deallocation function.
For details, please check Section 5.3.4 of the C++ 1998 Standard.
| pointer | pointer to the previously allocated memory | |
| file | null-terminated string of the file name | |
| line | line number |
| void operator delete | ( | void * | pointer | ) | throw () |
Deallocates memory.
| pointer | pointer to the previously allocated memory |
| void operator delete[] | ( | void * | pointer, | |
| const std::nothrow_t & | ||||
| ) | throw () |
Placement deallocation function.
For details, please check Section 5.3.4 of the C++ 1998 Standard.
| pointer | pointer to the previously allocated memory |
| void operator delete[] | ( | void * | pointer, | |
| const char * | file, | |||
| int | line | |||
| ) | throw () |
Placement deallocation function.
For details, please check Section 5.3.4 of the C++ 1998 Standard.
| pointer | pointer to the previously allocated memory | |
| file | null-terminated string of the file name | |
| line | line number |
| void operator delete[] | ( | void * | pointer | ) | throw () |
Deallocates array memory.
| pointer | pointer to the previously allocated memory |
| void* operator new | ( | size_t | size, | |
| const std::nothrow_t & | ||||
| ) | throw () |
Allocates memory with no-throw guarantee.
| size | size of the required memory block |
NULL if memory is insufficient | void* operator new | ( | size_t | size | ) | throw (std::bad_alloc) |
Allocates memory without file/line information.
| size | size of the required memory block |
NULL if memory is insufficient (_DEBUG_NEW_STD_OPER_NEW is 0) | bad_alloc | memory is insufficient (_DEBUG_NEW_STD_OPER_NEW is 1) |
| void* operator new | ( | size_t | size, | |
| const char * | file, | |||
| int | line | |||
| ) |
Allocates memory with file/line information.
| size | size of the required memory block | |
| file | null-terminated string of the file name | |
| line | line number |
NULL if memory is insufficient (_DEBUG_NEW_STD_OPER_NEW is 0) | bad_alloc | memory is insufficient (_DEBUG_NEW_STD_OPER_NEW is 1) |
| void* operator new[] | ( | size_t | size, | |
| const std::nothrow_t & | ||||
| ) | throw () |
Allocates array memory with no-throw guarantee.
| size | size of the required memory block |
NULL if memory is insufficient | void* operator new[] | ( | size_t | size | ) | throw (std::bad_alloc) |
Allocates array memory without file/line information.
| size | size of the required memory block |
NULL if memory is insufficient (_DEBUG_NEW_STD_OPER_NEW is 0) | bad_alloc | memory is insufficient (_DEBUG_NEW_STD_OPER_NEW is 1) |
| void* operator new[] | ( | size_t | size, | |
| const char * | file, | |||
| int | line | |||
| ) |
Allocates array memory with file/line information.
| size | size of the required memory block | |
| file | null-terminated string of the file name | |
| line | line number |
NULL if memory is insufficient (_DEBUG_NEW_STD_OPER_NEW is 0) | bad_alloc | memory is insufficient (_DEBUG_NEW_STD_OPER_NEW is 1) |
| static void print_position | ( | const void * | ptr, | |
| int | line | |||
| ) | [static] |
Prints the position information of a memory operation point.
When _DEBUG_NEW_USE_ADDR2LINE is defined to a non-zero value, this function will try to convert a given caller address to file/line information with addr2line.
| ptr | source file name if line is non-zero; caller address otherwise | |
| line | source line number if non-zero; indication that ptr is the caller address otherwise |
| static bool print_position_from_addr | ( | const void * | addr | ) | [static] |
Tries printing the position information from an instruction address.
This is the version that uses addr2line.
| addr | the instruction address to convert and print |
true if the address is converted successfully (and the result is printed); false if no useful information is got (and nothing is printed) | const int ALIGNED_LIST_ITEM_SIZE = ALIGN(sizeof(new_ptr_list_t)) |
The extra memory allocated by operator new.
| const unsigned MAGIC = 0x4442474E |
Definition of the constant magic number used for error detection.
| bool new_autocheck_flag = true |
Flag to control whether check_leaks will be automatically called on program exit.
| FILE* new_output_fp = stderr |
Pointer to the output stream.
The default output is stderr, and one may change it to a user stream if needed (say, new_verbose_flag is true and there are a lot of (de)allocations).
fast_mutex new_output_lock [static] |
The mutex guard to protect simultaneous output to new_output_fp.
| const char* new_progname = _DEBUG_NEW_PROGNAME |
Pointer to the program name.
Its initial value is the macro _DEBUG_NEW_PROGNAME. You should try to assign the program path to it early in your application. Assigning argv[0] to it in main is one way. If you use bash or ksh (or similar), the following statement is probably what you want: `new_progname = getenv("_");'.
new_ptr_list_t new_ptr_list [static] |
{
&new_ptr_list,
&new_ptr_list,
0,
{
""
},
0,
0,
MAGIC
}
List of all new'd pointers.
fast_mutex new_ptr_lock [static] |
The mutex guard to protect simultaneous access to the pointer list.
| bool new_verbose_flag = false |
Flag to control whether verbose messages are output.
| const size_t PLATFORM_MEM_ALIGNMENT = sizeof(size_t) * 2 |
The platform memory alignment.
The current value works well in platforms I have tested: Windows XP, Windows 7 x64, and Mac OS X Leopard. It may be smaller than the real alignment, but must be bigger than sizeof(size_t) for it work. __debug_new_recorder uses it to detect misaligned pointer returned by `new NonPODType[size]'.
size_t total_mem_alloc = 0 [static] |
Total memory allocated in bytes.
1.6.2