ofstream TRACEFILE__("TRACE.OUT");
#define cout TRACEFILE__
#endif
#endif // TRACE_H ///:~
Here’s a simple test of the previous file:
//: C03:Tracetst.cpp
// Test of trace.h
#include "../require.h"
#include
#include
using namespace std;
#define TRACEON
#include "Trace.h"
int main() {
ifstream f("Tracetst.cpp");
assure(f, "Tracetst.cpp");
cout << f.rdbuf(); // Dumps file contents to file
} ///:~
Finding memory leaks
The following straightforward debugging techniques are explained Volume 1.
1. For array bounds checking, use the Array template in C16:Array3.cpp of Volume 1 for all arrays. You can turn off the checking and increase efficiency when you’re ready to ship. (This doesn’t deal with the case of taking a pointer to an array, though—perhaps that could be made into a template somehow as well).
2. Check for non-virtual destructors in base classes.
Tracking new/delete and malloc/free
Common problems with memory allocation include mistakenly calling delete for memory not on the free store, deleting the free store more than once, and, most often, forgetting to delete such a pointer at all. This section discusses a system that can help you track down these kinds of problems.
As an
1. Even though it’s technically illegal, it works on many compilers.[26]
2. We illustrate some useful thinking along the way.
To use the memory checking system, you simply include the header file MemCheck.h, link the MemCheck.obj file into your application, so that all the calls to new and delete are intercepted, and call the macro MEM_ON( ) (explained later in this section) to initiate memory tracing. A trace of all allocations and deallocations is printed to the standard output (via stdout). When you use this system, all calls to new store information about the file and line where they were called. This is accomplished by using the
//: C02:MemCheck.h
#ifndef MEMCHECK_H
#define MEMCHECK_H
#include
// Hijack the new operator (both scalar and array versions)
void* operator new(std::size_t, const char*, long);
void* operator new[](std::size_t, const char*, long);
#define new new (__FILE__, __LINE__)
extern bool traceFlag;
#define TRACE_ON() traceFlag = true
#define TRACE_OFF() traceFlag = false
extern bool activeFlag;
#define MEM_ON() activeFlag = true
#define MEM_OFF() activeFlag = false
#endif
///:~
It is important that you include this file in any source file in which you want to track free store activity, but include it
In the following file, which contains the memory tracking implementation, everything is done with C standard I/O rather than with C++ iostreams. It shouldn’t make a difference, really, since we’re not interfering with iostreams’ use of the free store, but it’s safer to not take a chance. (Besides, we tried it. Some compilers complained, but all compilers were happy with the
//: C02:MemCheck.cpp {O}
#include
#include
#include
using namespace std;
#undef new
// Global flags set by macros in MemCheck.h
bool traceFlag = true;
bool activeFlag = false;
namespace {
// Memory map entry type
struct Info {
void* ptr;
const char* file;
long line;
};
// Memory map data
const size_t MAXPTRS = 10000u;
Info memMap[MAXPTRS];
size_t nptrs = 0;
// Searches the map for an address
int findPtr(void* p) {
for (int i = 0; i < nptrs; ++i)
if (memMap[i].ptr == p)
return i;
return -1;
}
void delPtr(void* p) {
int pos = findPtr(p);
assert(p >= 0);
// Remove pointer from map
for (size_t i = pos; i < nptrs-1; ++i)
memMap[i] = memMap[i+1];
--nptrs;
}
// Dummy type for static destructor