forked from zeromus/pj64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemTest.cpp
304 lines (259 loc) · 7.53 KB
/
MemTest.cpp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#pragma warning(disable:4786) //Disable std library warning
#pragma warning(disable:4530) //Disable std library warning
#include <map>
#include <string>
#include <crtdbg.h>
#include <malloc.h>
#include "MemTest.h"
#ifdef MEM_LEAK_TEST
typedef struct {
char File[300];
int line;
int size;
int order;
} DEBUG_LOCATION;
typedef std::map<void *, DEBUG_LOCATION> MEMLIST;
typedef MEMLIST::iterator MEMLIST_ITER;
class CMemList {
MEMLIST MemList;
HANDLE hSemaphone;
DWORD ThreadID;
enum INIT { Initialized = 123, NotInitialized };
INIT State;
int order;
public:
CMemList();
~CMemList();
void * AddItem ( size_t size, LPCSTR filename, int line );
void * AddItem ( size_t size, LPCSTR filename, int line, void * MemoryPtr );
void * ReAllocItem ( void * ptr, size_t size, LPCSTR filename, int line );
void removeItem ( void * ptr, bool FreePointer );
};
#include <shellapi.h> //Needed for ShellExecute
#pragma comment(lib, "shell32.lib") //Needed for ShellExecute
#undef new
#undef malloc
#undef realloc
#undef free
#undef VirtualAlloc
#undef VirtualFree
#ifndef MB_SERVICE_NOTIFICATION
#define MB_SERVICE_NOTIFICATION 0x00200000L
#endif
CMemList *MemList ( void ) {
static CMemList m_MemList;
return &m_MemList;
}
CMemList::CMemList() {
MemList.clear();
hSemaphone = CreateSemaphore(NULL, 1,1, NULL);
State = Initialized;
order = 0;
}
CMemList::~CMemList() {
int ItemsLeft = MemList.size();
if (ItemsLeft > 0) {
char path_buffer[_MAX_PATH], drive[_MAX_DRIVE] ,dir[_MAX_DIR];
char fname[_MAX_FNAME],ext[_MAX_EXT], LogFileName[_MAX_PATH];
GetModuleFileName(NULL,path_buffer,sizeof(path_buffer));
_splitpath( path_buffer, drive, dir, fname, ext );
_makepath( LogFileName, drive, dir, fname, "leak.csv" );
HANDLE hLogFile;
do
{
hLogFile = CreateFile(LogFileName,GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,
CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (hLogFile == INVALID_HANDLE_VALUE)
{
if (GetLastError() == ERROR_SHARING_VIOLATION) {
std::string Msg = std::string(LogFileName) + "\nCan not be opened for writing please close app using this file\n\nTry Again ?";
int Result = MessageBox(NULL,Msg.c_str(),"Memory Leak",MB_YESNO|MB_ICONQUESTION|MB_SETFOREGROUND | MB_SERVICE_NOTIFICATION);
if (Result == IDNO) {
break;
}
}
}
} while (hLogFile == INVALID_HANDLE_VALUE);
if (hLogFile != INVALID_HANDLE_VALUE)
{
SetFilePointer(hLogFile,0,NULL,FILE_BEGIN);
DWORD dwWritten;
char Msg[800];
_snprintf(Msg,sizeof(Msg),"Order, Source File, Line Number, Mem Size\r\n");
WriteFile( hLogFile,Msg,strlen(Msg),&dwWritten,NULL );
for (MEMLIST_ITER item = MemList.begin(); item != MemList.end(); item++)
{
_snprintf(Msg,sizeof(Msg),"%d,%s, %d, %d\r\n",
(*item).second.order,
(*item).second.File,
(*item).second.line,
(*item).second.size);
WriteFile( hLogFile,Msg,strlen(Msg),&dwWritten,NULL );
}
CloseHandle(hLogFile);
}
std::string Msg = std::string(fname) + std::string(ext) + "\n\nMemory Leaks detected\n\nOpen the Log File ?";
int Result = MessageBox(NULL,Msg.c_str(),"Memory Leak",MB_YESNO|MB_ICONQUESTION|MB_SETFOREGROUND| MB_SERVICE_NOTIFICATION);
if (Result == IDYES) {
ShellExecute(NULL,"open",LogFileName,NULL,NULL,SW_SHOW);
}
}
CloseHandle(hSemaphone);
hSemaphone = NULL;
State = NotInitialized;
}
void * CMemList::AddItem ( size_t size, LPCSTR filename, int line)
{
void *res = malloc(size);
if (res == NULL) {
_asm int 3
}
return AddItem(size,filename,line,res);
}
void * CMemList::AddItem ( size_t size, LPCSTR filename, int line, void * MemoryPtr )
{
__try {
if (State == Initialized && hSemaphone != NULL) {
DWORD CurrentThread = GetCurrentThreadId();
DWORD Result = WaitForSingleObject(hSemaphone,CurrentThread != ThreadID ? 30000: 0);
if (Result != WAIT_TIMEOUT) {
ThreadID = CurrentThread;
DEBUG_LOCATION info;
strncpy(info.File,filename,sizeof(info.File));
info.line = line;
info.size = size;
info.order = order++;
MemList.insert(MEMLIST::value_type(MemoryPtr,info));
long dwSemCount = 0;
ThreadID = -1;
ReleaseSemaphore(hSemaphone,1,&dwSemCount);
}
}
} __except (EXCEPTION_EXECUTE_HANDLER) {
_asm int 3
}
return MemoryPtr;
}
void * CMemList::ReAllocItem ( void * ptr, size_t size, LPCSTR filename, int line)
{
void *res = realloc(ptr, size);
if (res == NULL)
{
_asm int 3
}
if (ptr != res) {
__try {
if (State == Initialized && hSemaphone != NULL) {
DWORD CurrentThread = GetCurrentThreadId();
DWORD Result = WaitForSingleObject(hSemaphone,CurrentThread != ThreadID ? 30000 : 0);
if (Result != WAIT_TIMEOUT) {
ThreadID = CurrentThread;
//Add new pointer
DEBUG_LOCATION info;
strncpy(info.File,filename,sizeof(info.File));
info.line = line;
info.size = size;
info.order = order++;
MemList.insert(MEMLIST::value_type(res,info));
//remove old pointer
MEMLIST_ITER item = MemList.find(ptr);
if (item != MemList.end()) {
MemList.erase(ptr);
}
long dwSemCount = 0;
ThreadID = -1;
ReleaseSemaphore(hSemaphone,1,&dwSemCount);
}
}
} __except (EXCEPTION_EXECUTE_HANDLER) {
_asm int 3
}
}
return res;
}
void CMemList::removeItem (void * ptr, bool FreePointer )
{
if (FreePointer)
{
free(ptr);
}
__try {
if (State == Initialized && hSemaphone != NULL) {
DWORD CurrentThread = GetCurrentThreadId();
DWORD Result = WaitForSingleObject(hSemaphone,CurrentThread != ThreadID ? 30000 : 0);
if (Result != WAIT_TIMEOUT) {
ThreadID = CurrentThread;
MEMLIST_ITER item = MemList.find(ptr);
if (item != MemList.end()) {
MemList.erase(ptr);
}
long dwSemCount = 0;
ThreadID = -1;
ReleaseSemaphore(hSemaphone,1,&dwSemCount);
}
}
}__except(EXCEPTION_EXECUTE_HANDLER ){
_asm int 3
}
}
void MemTest_AddLeak(char* Comment) {
MemList()->AddItem(1,Comment,-1);
}
void* MemTest_malloc (size_t size, char* filename, int line) {
return MemList()->AddItem(size,filename,line);
}
void* MemTest_realloc (void* ptr, size_t size, char* filename, int line) {
return MemList()->ReAllocItem(ptr, size,filename,line);
}
void MemTest_free(void* ptr)
{
MemList()->removeItem(ptr,true);
}
void* operator new (size_t size, char* filename, int line)
{
return MemList()->AddItem(size,filename,line);
}
void* operator new (size_t size)
{
return MemList()->AddItem(size,"Unknown",0);
}
void* operator new [] (size_t size, char* filename, int line)
{
return MemList()->AddItem(size,filename,line);
}
void* operator new [] (size_t size)
{
return MemList()->AddItem(size,"Unknown",0);
}
void operator delete ( void* ptr)
{
MemList()->removeItem(ptr,true);
}
void operator delete[](void* ptr)
{
delete ptr;
}
LPVOID MemTest_VirtualAlloc(
LPVOID lpAddress, // region to reserve or commit
SIZE_T dwSize, // size of region
DWORD flAllocationType, // type of allocation
DWORD flProtect, // type of access protection
LPCSTR filename,
int line)
{
LPVOID ptr = VirtualAlloc(lpAddress,dwSize,flAllocationType,flProtect);
if (ptr && lpAddress == NULL && (flAllocationType & MEM_RESERVE) != 0)
{
MemList()->AddItem(dwSize,filename,line,ptr);
}
return ptr;
}
BOOL MemTest_VirtualFree( LPVOID lpAddress, SIZE_T dwSize, DWORD dwFreeType )
{
if ((dwFreeType & MEM_RELEASE) != 0)
{
MemList()->removeItem(lpAddress,false);
}
return VirtualFree(lpAddress,dwSize,dwFreeType);
}
#endif