[Project64] Code cleanup of MemTest.cpp

This commit is contained in:
zilmar 2015-12-23 13:10:04 +11:00
parent a05c3f5c00
commit 6395ea52f7
1 changed files with 233 additions and 224 deletions

View File

@ -1,10 +1,11 @@
#include "stdafx.h"
#if defined(_MSC_VER) #if defined(_MSC_VER)
#include <crtdbg.h> #include <crtdbg.h>
#else #else
#define _ASSERTE(expr) ((void)0) #define _ASSERTE(expr) ((void)0)
#endif #endif
#include "stdafx.h"
#ifdef _DEBUG #ifdef _DEBUG
#pragma warning(disable:4786) //Disable std library warning #pragma warning(disable:4786) //Disable std library warning
@ -17,7 +18,6 @@
#include <shellapi.h> //Needed for ShellExecute #include <shellapi.h> //Needed for ShellExecute
#pragma comment(lib, "shell32.lib") //Needed for ShellExecute #pragma comment(lib, "shell32.lib") //Needed for ShellExecute
#include <tchar.h>
#undef new #undef new
#undef malloc #undef malloc
#undef realloc #undef realloc
@ -29,309 +29,318 @@
#define MB_SERVICE_NOTIFICATION 0x00200000L #define MB_SERVICE_NOTIFICATION 0x00200000L
#endif #endif
CMemList *MemList ( void ) CMemList *MemList(void)
{ {
static CMemList m_MemList; static CMemList m_MemList;
return &m_MemList; return &m_MemList;
} }
CMemList::CMemList() CMemList::CMemList()
{ {
MemList.clear(); MemList.clear();
hSemaphone = CreateSemaphore(NULL, 1,1, NULL); hSemaphone = CreateSemaphore(NULL, 1, 1, NULL);
State = Initialized; State = Initialized;
order = 0; order = 0;
LogAllocate = false; LogAllocate = false;
ThreadID = 0; ThreadID = 0;
m_hModule = NULL; m_hModule = NULL;
for (UINT_PTR TestLoc = ((UINT_PTR)::MemList) & ~0xFFF; TestLoc != 0; TestLoc -= 0x1000) for (UINT_PTR TestLoc = ((UINT_PTR)::MemList) & ~0xFFF; TestLoc != 0; TestLoc -= 0x1000)
{ {
WORD HeaderID = *(WORD *)TestLoc; WORD HeaderID = *(WORD *)TestLoc;
if (HeaderID != 'ZM') if (HeaderID != 'ZM')
{ {
continue; continue;
} }
m_hModule = (HMODULE)TestLoc; m_hModule = (HMODULE)TestLoc;
break; break;
} }
} }
CMemList::~CMemList() CMemList::~CMemList()
{ {
size_t ItemsLeft = MemList.size(); size_t ItemsLeft = MemList.size();
if (ItemsLeft > 0) if (ItemsLeft > 0)
{ {
TCHAR path_buffer[_MAX_PATH], drive[_MAX_DRIVE], dir[_MAX_DIR]; TCHAR path_buffer[_MAX_PATH], drive[_MAX_DRIVE], dir[_MAX_DIR];
TCHAR fname[_MAX_FNAME], ext[_MAX_EXT], LogFileName[_MAX_PATH]; TCHAR fname[_MAX_FNAME], ext[_MAX_EXT], LogFileName[_MAX_PATH];
memset(path_buffer, 0, sizeof(path_buffer)); memset(path_buffer, 0, sizeof(path_buffer));
memset(drive, 0, sizeof(drive)); memset(drive, 0, sizeof(drive));
memset(dir, 0, sizeof(dir)); memset(dir, 0, sizeof(dir));
memset(fname, 0, sizeof(fname)); memset(fname, 0, sizeof(fname));
memset(ext, 0, sizeof(ext)); memset(ext, 0, sizeof(ext));
memset(LogFileName, 0, sizeof(LogFileName)); memset(LogFileName, 0, sizeof(LogFileName));
GetModuleFileName(m_hModule,path_buffer,sizeof(path_buffer)); GetModuleFileName(m_hModule, path_buffer, sizeof(path_buffer));
_tsplitpath( path_buffer, drive, dir, fname, ext ); _splitpath(path_buffer, drive, dir, fname, ext);
_tmakepath( LogFileName, drive, dir, fname, _T("leak.csv") ); _makepath(LogFileName, drive, dir, fname, "leak.csv");
HANDLE hLogFile = INVALID_HANDLE_VALUE; HANDLE hLogFile = INVALID_HANDLE_VALUE;
do do
{ {
hLogFile = CreateFile( hLogFile = CreateFile(
LogFileName, LogFileName,
GENERIC_WRITE, GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, NULL,
CREATE_ALWAYS, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
NULL NULL
); );
if (hLogFile == INVALID_HANDLE_VALUE) if (hLogFile == INVALID_HANDLE_VALUE)
{ {
if (GetLastError() == ERROR_SHARING_VIOLATION) if (GetLastError() == ERROR_SHARING_VIOLATION)
{ {
TCHAR Msg[3000]; char Msg[3000];
_stprintf(Msg,TEXT("%s\nCan not be opened for writing please close app using this file\n\nTry Again ?"),LogFileName); sprintf(Msg, "%s\nCan not be opened for writing please close app using this file\n\nTry Again ?", LogFileName);
int Result = MessageBox(NULL,Msg,_T("Memory Leak"),MB_YESNO|MB_ICONQUESTION|MB_SETFOREGROUND | MB_SERVICE_NOTIFICATION); int Result = MessageBox(NULL, Msg, "Memory Leak", MB_YESNO | MB_ICONQUESTION | MB_SETFOREGROUND | MB_SERVICE_NOTIFICATION);
if (Result == IDNO) if (Result == IDNO)
{ {
break; break;
} }
} }
} }
} while (hLogFile == INVALID_HANDLE_VALUE); } while (hLogFile == INVALID_HANDLE_VALUE);
if (hLogFile != INVALID_HANDLE_VALUE) if (hLogFile != INVALID_HANDLE_VALUE)
{ {
SetFilePointer(hLogFile,0,NULL,FILE_BEGIN); SetFilePointer(hLogFile, 0, NULL, FILE_BEGIN);
DWORD dwWritten = 0; DWORD dwWritten = 0;
char Msg[800]; char Msg[800];
_snprintf(Msg,sizeof(Msg),"Order, Source File, Line Number, Mem Size\r\n"); _snprintf(Msg, sizeof(Msg), "Order, Source File, Line Number, Mem Size\r\n");
WriteFile( hLogFile,Msg,(DWORD)strlen(Msg),&dwWritten,NULL ); WriteFile(hLogFile, Msg, (DWORD)strlen(Msg), &dwWritten, NULL);
for (MEMLIST_ITER item = MemList.begin(); item != MemList.end(); item++) for (MEMLIST_ITER item = MemList.begin(); item != MemList.end(); item++)
{ {
_snprintf(Msg,sizeof(Msg),"%d,%s, %d, %d\r\n", _snprintf(Msg, sizeof(Msg), "%d,%s, %d, %d\r\n",
(*item).second.order, (*item).second.order,
(*item).second.File, (*item).second.File,
(*item).second.line, (*item).second.line,
(*item).second.size); (*item).second.size);
WriteFile( hLogFile,Msg,(DWORD)strlen(Msg),&dwWritten,NULL ); WriteFile(hLogFile, Msg, (DWORD)strlen(Msg), &dwWritten, NULL);
} }
CloseHandle(hLogFile); CloseHandle(hLogFile);
} }
TCHAR Msg[3000]; char Msg[3000];
_stprintf(Msg,TEXT("%s%s\n\nMemory Leaks detected\n\nOpen the Log File ?"),fname,ext); sprintf(Msg, "%s%s\n\nMemory Leaks detected\n\nOpen the Log File ?", fname, ext);
int Result = MessageBox(NULL,Msg,_T("Memory Leak"),MB_YESNO|MB_ICONQUESTION|MB_SETFOREGROUND| MB_SERVICE_NOTIFICATION); int Result = MessageBox(NULL, Msg, "Memory Leak", MB_YESNO | MB_ICONQUESTION | MB_SETFOREGROUND | MB_SERVICE_NOTIFICATION);
if (Result == IDYES) if (Result == IDYES)
{ {
ShellExecute(NULL,_T("open"),LogFileName,NULL,NULL,SW_SHOW); ShellExecute(NULL, "open", LogFileName, NULL, NULL, SW_SHOW);
} }
} }
CloseHandle(hSemaphone); CloseHandle(hSemaphone);
hSemaphone = NULL; hSemaphone = NULL;
State = NotInitialized; State = NotInitialized;
} }
void * CMemList::AddItem ( size_t size, char * filename, int line) void * CMemList::AddItem(size_t size, char * filename, int line)
{ {
void *res = malloc(size); void *res = malloc(size);
if (res == NULL) if (res == NULL)
{ {
return res; return res;
} }
RecordAddItem(res,size,filename,line); RecordAddItem(res, size, filename, line);
return res; return res;
} }
void CMemList::RecordAddItem ( void * ptr, size_t size, const char * filename, int line) void CMemList::RecordAddItem(void * ptr, size_t size, const char * filename, int line)
{ {
__try { __try
if (State == Initialized && hSemaphone != NULL) {
{ if (State == Initialized && hSemaphone != NULL)
DWORD CurrentThread = GetCurrentThreadId(); {
DWORD Result = WaitForSingleObject(hSemaphone,CurrentThread != ThreadID ? 30000: 0); DWORD CurrentThread = GetCurrentThreadId();
if (Result != WAIT_TIMEOUT) DWORD Result = WaitForSingleObject(hSemaphone, CurrentThread != ThreadID ? 30000 : 0);
{ if (Result != WAIT_TIMEOUT)
ThreadID = CurrentThread; {
ThreadID = CurrentThread;
DEBUG_LOCATION info;
strncpy(info.File,filename,sizeof(info.File));
info.line = line;
info.size = (int)size;
info.order = order++;
Insert(ptr, info); DEBUG_LOCATION info;
strncpy(info.File, filename, sizeof(info.File));
info.line = line;
info.size = (int)size;
info.order = order++;
long dwSemCount = 0; Insert(ptr, info);
ThreadID = (DWORD)-1;
ReleaseSemaphore(hSemaphone,1,&dwSemCount); long dwSemCount = 0;
} ThreadID = (DWORD)-1;
} ReleaseSemaphore(hSemaphone, 1, &dwSemCount);
} __except (EXCEPTION_EXECUTE_HANDLER) { }
//_asm int 3 }
} }
__except (EXCEPTION_EXECUTE_HANDLER)
{
//_asm int 3
}
} }
void CMemList::Insert(void *res, DEBUG_LOCATION &info) void CMemList::Insert(void *res, DEBUG_LOCATION &info)
{ {
MemList.insert(MEMLIST::value_type(res,info)); MemList.insert(MEMLIST::value_type(res, info));
} }
void * CMemList::ReAllocItem ( void * ptr, size_t size, const char * filename, int line) void * CMemList::ReAllocItem(void * ptr, size_t size, const char * filename, int line)
{ {
void *res = realloc(ptr, size); void *res = realloc(ptr, size);
if (res == NULL) if (res == NULL)
{ {
return res; return res;
} }
if (ptr != res) if (ptr != res)
{ {
__try { __try
if (State == Initialized && hSemaphone != NULL) {
{ if (State == Initialized && hSemaphone != NULL)
DWORD CurrentThread = GetCurrentThreadId(); {
DWORD Result = WaitForSingleObject(hSemaphone,CurrentThread != ThreadID ? 30000 : 0); DWORD CurrentThread = GetCurrentThreadId();
if (Result != WAIT_TIMEOUT) DWORD Result = WaitForSingleObject(hSemaphone, CurrentThread != ThreadID ? 30000 : 0);
{ if (Result != WAIT_TIMEOUT)
ThreadID = CurrentThread; {
//Add new pointer ThreadID = CurrentThread;
DEBUG_LOCATION info; //Add new pointer
strncpy(info.File,filename,sizeof(info.File)); DEBUG_LOCATION info;
info.line = line; strncpy(info.File, filename, sizeof(info.File));
info.size = (int)size; info.line = line;
info.order = order++; info.size = (int)size;
info.order = order++;
Insert(res, info); Insert(res, info);
//remove old pointer //remove old pointer
Remove(ptr); Remove(ptr);
long dwSemCount = 0; long dwSemCount = 0;
ThreadID = (DWORD)-1; ThreadID = (DWORD)-1;
ReleaseSemaphore(hSemaphone,1,&dwSemCount); ReleaseSemaphore(hSemaphone, 1, &dwSemCount);
} }
} }
} __except (EXCEPTION_EXECUTE_HANDLER) { }
//_asm int 3 __except (EXCEPTION_EXECUTE_HANDLER)
} {
} //_asm int 3
return res; }
}
return res;
} }
void CMemList::Remove(void *ptr) void CMemList::Remove(void *ptr)
{ {
//remove old pointer //remove old pointer
MEMLIST_ITER item = MemList.find(ptr); MEMLIST_ITER item = MemList.find(ptr);
if (item != MemList.end()) if (item != MemList.end())
{ {
MemList.erase(ptr); MemList.erase(ptr);
} }
} }
void CMemList::removeItem (void * ptr, bool bFree ) void CMemList::removeItem(void * ptr, bool bFree)
{ {
if (bFree) if (bFree)
{ {
free(ptr); free(ptr);
} }
__try { __try
if (State == Initialized && hSemaphone != NULL) {
{ if (State == Initialized && hSemaphone != NULL)
DWORD CurrentThread = GetCurrentThreadId(); {
DWORD Result = WaitForSingleObject(hSemaphone,CurrentThread != ThreadID ? 30000 : 0); DWORD CurrentThread = GetCurrentThreadId();
if (Result != WAIT_TIMEOUT) DWORD Result = WaitForSingleObject(hSemaphone, CurrentThread != ThreadID ? 30000 : 0);
{ if (Result != WAIT_TIMEOUT)
ThreadID = CurrentThread; {
ThreadID = CurrentThread;
Remove(ptr);
long dwSemCount = 0; Remove(ptr);
ThreadID = (DWORD)-1;
ReleaseSemaphore(hSemaphone,1,&dwSemCount); long dwSemCount = 0;
} ThreadID = (DWORD)-1;
} ReleaseSemaphore(hSemaphone, 1, &dwSemCount);
}__except(EXCEPTION_EXECUTE_HANDLER ){ }
//_asm int 3 }
} }
__except (EXCEPTION_EXECUTE_HANDLER)
{
//_asm int 3
}
} }
void MemTest_AddLeak(char* Comment) void MemTest_AddLeak(char* Comment)
{ {
MemList()->AddItem(1,Comment,-1); MemList()->AddItem(1, Comment, -1);
} }
void* MemTest_malloc (size_t size, char* filename, int line) void* MemTest_malloc(size_t size, char* filename, int line)
{ {
return MemList()->AddItem(size,filename,line); return MemList()->AddItem(size, filename, line);
} }
void* MemTest_realloc (void* ptr, size_t size, char* filename, int line) void* MemTest_realloc(void* ptr, size_t size, char* filename, int line)
{ {
return MemList()->ReAllocItem(ptr, size,filename,line); return MemList()->ReAllocItem(ptr, size, filename, line);
} }
void* operator new (size_t size, char* filename, int line) void* operator new (size_t size, char* filename, int line)
{ {
return MemList()->AddItem(size,filename,line); return MemList()->AddItem(size, filename, line);
} }
void* operator new (size_t size) void* operator new (size_t size)
{ {
return MemList()->AddItem(size,"Unknown",0); return MemList()->AddItem(size, "Unknown", 0);
} }
void* operator new [] (size_t size, char* filename, int line) void* operator new [](size_t size, char* filename, int line)
{ {
return MemList()->AddItem(size,filename,line); return MemList()->AddItem(size, filename, line);
} }
void* operator new [] (size_t size) void* operator new [](size_t size)
{ {
return MemList()->AddItem(size,"Unknown",0); return MemList()->AddItem(size, "Unknown", 0);
} }
void operator delete ( void* ptr) void operator delete (void* ptr)
{ {
MemList()->removeItem(ptr,true); MemList()->removeItem(ptr, true);
} }
void operator delete[](void* ptr) void operator delete[](void* ptr)
{ {
delete ptr; delete ptr;
} }
LPVOID MemTest_VirtualAlloc( LPVOID MemTest_VirtualAlloc(
LPVOID lpAddress, // region to reserve or commit LPVOID lpAddress, // region to reserve or commit
SIZE_T dwSize, // size of region SIZE_T dwSize, // size of region
DWORD flAllocationType, // type of allocation DWORD flAllocationType, // type of allocation
DWORD flProtect, // type of access protection DWORD flProtect, // type of access protection
LPCSTR filename, LPCSTR filename,
int line) int line)
{ {
LPVOID ptr = VirtualAlloc(lpAddress,dwSize,flAllocationType,flProtect); LPVOID ptr = VirtualAlloc(lpAddress, dwSize, flAllocationType, flProtect);
if (ptr && lpAddress == NULL && (flAllocationType & MEM_RESERVE) != 0) if (ptr && lpAddress == NULL && (flAllocationType & MEM_RESERVE) != 0)
{ {
MemList()->RecordAddItem(ptr,dwSize,filename,line); MemList()->RecordAddItem(ptr, dwSize, filename, line);
} }
return ptr; return ptr;
} }
BOOL MemTest_VirtualFree( LPVOID lpAddress, SIZE_T dwSize, DWORD dwFreeType ) BOOL MemTest_VirtualFree(LPVOID lpAddress, SIZE_T dwSize, DWORD dwFreeType)
{ {
if ((dwFreeType & MEM_RELEASE) != 0) if ((dwFreeType & MEM_RELEASE) != 0)
{ {
MemList()->removeItem(lpAddress,false); MemList()->removeItem(lpAddress, false);
} }
return VirtualFree(lpAddress,dwSize,dwFreeType); return VirtualFree(lpAddress, dwSize, dwFreeType);
} }
#endif #endif
#endif #endif