codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include "leakdetection.h" #include <stdio.h> #include <iostream> #include <list> using namespace std; void AddTrack(DWORD addr, DWORD asize, const char *fname, DWORD lnum); void RemoveTrack(DWORD addr); typedef struct { DWORD address; DWORD size; char file[255]; DWORD line; } ALLOC_INFO; typedef list<ALLOC_INFO*> AllocList; AllocList *allocList; void AddTrack(DWORD addr, DWORD asize, const char *fname, DWORD lnum) { ALLOC_INFO *info; if(!allocList) { allocList = new(AllocList); } info = new(ALLOC_INFO); info->address = addr; strncpy(info->file, fname, 255); info->line = lnum; info->size = asize; allocList->insert(allocList->begin(), info); }; void RemoveTrack(DWORD addr) { AllocList::iterator i; if(!allocList) return; for(i = allocList->begin(); i != allocList->end(); i++) { if((*i)->address == addr) { allocList->remove((*i)); break; } } }; void DumpUnfreed() { AllocList::iterator i; DWORD totalSize = 0; char buf[1024]; if(!allocList) return; for(i = allocList->begin(); i != allocList->end(); i++) { sprintf(buf, "%-20s:\tLINE %d,\tADDRESS %d\t%d unfreed\n", (*i)->file, (*i)->line, (*i)->address, (*i)->size); cout << buf; totalSize += (*i)->size; } sprintf(buf, "---------------------------------------------\n"); cout << buf; sprintf(buf, "Total Unfreed: %d bytes\n", totalSize); cout << buf; }; inline void * operator new(DWORD size, const char *file, int line) { void *ptr = (void *)malloc(size); AddTrack((DWORD)ptr, size, file, line); return(ptr); }; void operator delete (void *p) throw() { RemoveTrack((DWORD)p); free(p); }; // Overload array new void* operator new[](DWORD siz, const char* file, long line) { return operator new(siz, file, line); } // Override scalar delete //void operator delete (void *p) //{ // free(p); //} //void operator delete(void* p) { // RemoveTrack((DWORD)p); // free(p); //} // Override array delete void operator delete[](void* p) throw() { operator delete(p); } ///:~ //#define DEBUG_NEW new(__FILE__, __LINE__) //#define new DEBUG_NEW
Private
[
?
]
Run code
Submit