1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#ifndef __SEXYMEMMGR_H__
#define __SEXYMEMMGR_H__
//////////////////////////////////////////////////////////////////////////
// HOW TO USE THIS FILE
//
// In the desired .CPP file (NOT header file), AFTER ALL of your
// #include declarations, do a #include "memmgr.h" or whatever you renamed
// this file to. It's very important that you do it only in the .cpp and
// after every other include file, otherwise it won't compile. The memory leaks
// will appear in a file called mem_leaks.txt and they will also be printed out
// in the output window when the program exits.
//
//////////////////////////////////////////////////////////////////////////
#include <list>
#include <stdio.h>
#include <stdlib.h>
extern void SexyDumpUnfreed();
#if defined(SEXY_MEMTRACE) && !defined(RELEASEFINAL)
/************************************************************************/
/* DO NOT CALL THESE TWO METHODS DIRECTLY */
/************************************************************************/
void SexyMemAddTrack(void* addr, int asize, const char *fname, int lnum);
void SexyMemRemoveTrack(void *addr);
//Replacement for the standard "new" operator, records size of allocation and
//the file/line number it was on
inline void* __cdecl operator new(unsigned int size, const char* file, int line)
{
void* ptr = (void*)malloc(size);
SexyMemAddTrack(ptr, size, file, line);
return(ptr);
}
//Same as above, but for arrays
inline void* __cdecl operator new[](unsigned int size, const char* file, int line)
{
void* ptr = (void*)malloc(size);
SexyMemAddTrack(ptr, size, file, line);
return(ptr);
}
// These single argument new operators allow vc6 apps to compile without errors
inline void* __cdecl operator new(unsigned int size)
{
void* ptr = (void*)malloc(size);
return(ptr);
}
inline void* __cdecl operator new[](unsigned int size)
{
void* ptr = (void*)malloc(size);
return(ptr);
}
//custom delete operators
inline void __cdecl operator delete(void* p)
{
SexyMemRemoveTrack(p);
free(p);
}
inline void __cdecl operator delete[](void* p)
{
SexyMemRemoveTrack(p);
free(p);
}
//needed in case in the constructor of the class we're newing, it throws an exception
inline void __cdecl operator delete(void* pMem, const char *file, int line)
{
free(pMem);
}
inline void __cdecl operator delete[](void* pMem, const char *file, int line)
{
free(pMem);
}
#define DEBUG_NEW new(__FILE__, __LINE__)
#define new DEBUG_NEW
#endif // SEXY_MEMTRACE
#endif
|