[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)
#include <crtdbg.h>
#else
#define _ASSERTE(expr) ((void)0)
#endif
#include "stdafx.h"
#ifdef _DEBUG
#pragma warning(disable:4786) //Disable std library warning
@ -17,7 +18,6 @@
#include <shellapi.h> //Needed for ShellExecute
#pragma comment(lib, "shell32.lib") //Needed for ShellExecute
#include <tchar.h>
#undef new
#undef malloc
#undef realloc
@ -29,7 +29,7 @@
#define MB_SERVICE_NOTIFICATION 0x00200000L
#endif
CMemList *MemList ( void )
CMemList *MemList(void)
{
static CMemList m_MemList;
@ -39,7 +39,7 @@ CMemList *MemList ( void )
CMemList::CMemList()
{
MemList.clear();
hSemaphone = CreateSemaphore(NULL, 1,1, NULL);
hSemaphone = CreateSemaphore(NULL, 1, 1, NULL);
State = Initialized;
order = 0;
LogAllocate = false;
@ -73,10 +73,10 @@ CMemList::~CMemList()
memset(ext, 0, sizeof(ext));
memset(LogFileName, 0, sizeof(LogFileName));
GetModuleFileName(m_hModule,path_buffer,sizeof(path_buffer));
_tsplitpath( path_buffer, drive, dir, fname, ext );
GetModuleFileName(m_hModule, path_buffer, sizeof(path_buffer));
_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;
do
@ -94,9 +94,9 @@ CMemList::~CMemList()
{
if (GetLastError() == ERROR_SHARING_VIOLATION)
{
TCHAR Msg[3000];
_stprintf(Msg,TEXT("%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);
char Msg[3000];
sprintf(Msg, "%s\nCan not be opened for writing please close app using this file\n\nTry Again ?", LogFileName);
int Result = MessageBox(NULL, Msg, "Memory Leak", MB_YESNO | MB_ICONQUESTION | MB_SETFOREGROUND | MB_SERVICE_NOTIFICATION);
if (Result == IDNO)
{
break;
@ -107,30 +107,30 @@ CMemList::~CMemList()
if (hLogFile != INVALID_HANDLE_VALUE)
{
SetFilePointer(hLogFile,0,NULL,FILE_BEGIN);
SetFilePointer(hLogFile, 0, NULL, FILE_BEGIN);
DWORD dwWritten = 0;
char Msg[800];
_snprintf(Msg,sizeof(Msg),"Order, Source File, Line Number, Mem Size\r\n");
WriteFile( hLogFile,Msg,(DWORD)strlen(Msg),&dwWritten,NULL );
_snprintf(Msg, sizeof(Msg), "Order, Source File, Line Number, Mem Size\r\n");
WriteFile(hLogFile, Msg, (DWORD)strlen(Msg), &dwWritten, NULL);
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.File,
(*item).second.line,
(*item).second.size);
WriteFile( hLogFile,Msg,(DWORD)strlen(Msg),&dwWritten,NULL );
WriteFile(hLogFile, Msg, (DWORD)strlen(Msg), &dwWritten, NULL);
}
CloseHandle(hLogFile);
}
TCHAR Msg[3000];
_stprintf(Msg,TEXT("%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);
char Msg[3000];
sprintf(Msg, "%s%s\n\nMemory Leaks detected\n\nOpen the Log File ?", fname, ext);
int Result = MessageBox(NULL, Msg, "Memory Leak", MB_YESNO | MB_ICONQUESTION | MB_SETFOREGROUND | MB_SERVICE_NOTIFICATION);
if (Result == IDYES)
{
ShellExecute(NULL,_T("open"),LogFileName,NULL,NULL,SW_SHOW);
ShellExecute(NULL, "open", LogFileName, NULL, NULL, SW_SHOW);
}
}
CloseHandle(hSemaphone);
@ -138,30 +138,31 @@ CMemList::~CMemList()
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);
if (res == NULL)
{
return res;
}
RecordAddItem(res,size,filename,line);
RecordAddItem(res, size, filename, line);
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)
{
DWORD CurrentThread = GetCurrentThreadId();
DWORD Result = WaitForSingleObject(hSemaphone,CurrentThread != ThreadID ? 30000: 0);
DWORD Result = WaitForSingleObject(hSemaphone, CurrentThread != ThreadID ? 30000 : 0);
if (Result != WAIT_TIMEOUT)
{
ThreadID = CurrentThread;
DEBUG_LOCATION info;
strncpy(info.File,filename,sizeof(info.File));
strncpy(info.File, filename, sizeof(info.File));
info.line = line;
info.size = (int)size;
info.order = order++;
@ -170,20 +171,22 @@ void CMemList::RecordAddItem ( void * ptr, size_t size, const char * filename, i
long dwSemCount = 0;
ThreadID = (DWORD)-1;
ReleaseSemaphore(hSemaphone,1,&dwSemCount);
ReleaseSemaphore(hSemaphone, 1, &dwSemCount);
}
}
} __except (EXCEPTION_EXECUTE_HANDLER) {
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
//_asm int 3
}
}
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);
if (res == NULL)
@ -192,17 +195,18 @@ void * CMemList::ReAllocItem ( void * ptr, size_t size, const char * filename, i
}
if (ptr != res)
{
__try {
__try
{
if (State == Initialized && hSemaphone != NULL)
{
DWORD CurrentThread = GetCurrentThreadId();
DWORD Result = WaitForSingleObject(hSemaphone,CurrentThread != ThreadID ? 30000 : 0);
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));
strncpy(info.File, filename, sizeof(info.File));
info.line = line;
info.size = (int)size;
info.order = order++;
@ -214,10 +218,12 @@ void * CMemList::ReAllocItem ( void * ptr, size_t size, const char * filename, i
long dwSemCount = 0;
ThreadID = (DWORD)-1;
ReleaseSemaphore(hSemaphone,1,&dwSemCount);
ReleaseSemaphore(hSemaphone, 1, &dwSemCount);
}
}
} __except (EXCEPTION_EXECUTE_HANDLER) {
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
//_asm int 3
}
}
@ -234,18 +240,19 @@ void CMemList::Remove(void *ptr)
}
}
void CMemList::removeItem (void * ptr, bool bFree )
void CMemList::removeItem(void * ptr, bool bFree)
{
if (bFree)
{
free(ptr);
}
__try {
__try
{
if (State == Initialized && hSemaphone != NULL)
{
DWORD CurrentThread = GetCurrentThreadId();
DWORD Result = WaitForSingleObject(hSemaphone,CurrentThread != ThreadID ? 30000 : 0);
DWORD Result = WaitForSingleObject(hSemaphone, CurrentThread != ThreadID ? 30000 : 0);
if (Result != WAIT_TIMEOUT)
{
ThreadID = CurrentThread;
@ -254,52 +261,54 @@ void CMemList::removeItem (void * ptr, bool bFree )
long dwSemCount = 0;
ThreadID = (DWORD)-1;
ReleaseSemaphore(hSemaphone,1,&dwSemCount);
ReleaseSemaphore(hSemaphone, 1, &dwSemCount);
}
}
}__except(EXCEPTION_EXECUTE_HANDLER ){
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
//_asm int 3
}
}
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)
{
return MemList()->AddItem(size,filename,line);
return MemList()->AddItem(size, filename, line);
}
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)
@ -308,29 +317,29 @@ void operator delete[](void* 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 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);
LPVOID ptr = VirtualAlloc(lpAddress, dwSize, flAllocationType, flProtect);
if (ptr && lpAddress == NULL && (flAllocationType & MEM_RESERVE) != 0)
{
MemList()->RecordAddItem(ptr,dwSize,filename,line);
MemList()->RecordAddItem(ptr, dwSize, filename, line);
}
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)
{
MemList()->removeItem(lpAddress,false);
MemList()->removeItem(lpAddress, false);
}
return VirtualFree(lpAddress,dwSize,dwFreeType);
return VirtualFree(lpAddress, dwSize, dwFreeType);
}
#endif