2015-11-17 18:31:36 +00:00
|
|
|
#if defined(_MSC_VER)
|
2008-09-18 03:15:49 +00:00
|
|
|
#include <crtdbg.h>
|
2015-11-17 18:31:36 +00:00
|
|
|
#else
|
|
|
|
#define _ASSERTE(expr) ((void)0)
|
|
|
|
#endif
|
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#ifdef _DEBUG
|
|
|
|
|
|
|
|
#pragma warning(disable:4786) //Disable std library warning
|
|
|
|
#pragma warning(disable:4530) //Disable std library warning
|
|
|
|
#include <string>
|
|
|
|
#include <malloc.h>
|
|
|
|
|
|
|
|
#include "MemTest.h"
|
|
|
|
#ifdef MEM_LEAK_TEST
|
|
|
|
|
|
|
|
#include <shellapi.h> //Needed for ShellExecute
|
|
|
|
#pragma comment(lib, "shell32.lib") //Needed for ShellExecute
|
2010-09-22 21:43:42 +00:00
|
|
|
#include <tchar.h>
|
2008-09-18 03:15:49 +00:00
|
|
|
#undef new
|
|
|
|
#undef malloc
|
|
|
|
#undef realloc
|
|
|
|
#undef free
|
|
|
|
#undef VirtualAlloc
|
|
|
|
#undef VirtualFree
|
|
|
|
|
|
|
|
#ifndef MB_SERVICE_NOTIFICATION
|
|
|
|
#define MB_SERVICE_NOTIFICATION 0x00200000L
|
|
|
|
#endif
|
|
|
|
|
2015-03-17 21:19:42 +00:00
|
|
|
CMemList *MemList ( void )
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
static CMemList m_MemList;
|
|
|
|
|
|
|
|
return &m_MemList;
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:19:42 +00:00
|
|
|
CMemList::CMemList()
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
MemList.clear();
|
|
|
|
hSemaphone = CreateSemaphore(NULL, 1,1, NULL);
|
|
|
|
State = Initialized;
|
|
|
|
order = 0;
|
2010-09-22 21:43:42 +00:00
|
|
|
LogAllocate = false;
|
|
|
|
ThreadID = 0;
|
|
|
|
m_hModule = NULL;
|
|
|
|
|
|
|
|
for (UINT_PTR TestLoc = ((UINT_PTR)::MemList) & ~0xFFF; TestLoc != 0; TestLoc -= 0x1000)
|
|
|
|
{
|
|
|
|
WORD HeaderID = *(WORD *)TestLoc;
|
|
|
|
if (HeaderID != 'ZM')
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
m_hModule = (HMODULE)TestLoc;
|
|
|
|
break;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 21:19:42 +00:00
|
|
|
CMemList::~CMemList()
|
|
|
|
{
|
2010-09-22 21:43:42 +00:00
|
|
|
size_t ItemsLeft = MemList.size();
|
2015-03-17 21:19:42 +00:00
|
|
|
if (ItemsLeft > 0)
|
|
|
|
{
|
|
|
|
TCHAR path_buffer[_MAX_PATH], drive[_MAX_DRIVE], dir[_MAX_DIR];
|
|
|
|
TCHAR fname[_MAX_FNAME], ext[_MAX_EXT], LogFileName[_MAX_PATH];
|
2010-09-22 21:43:42 +00:00
|
|
|
|
|
|
|
memset(path_buffer, 0, sizeof(path_buffer));
|
|
|
|
memset(drive, 0, sizeof(drive));
|
|
|
|
memset(dir, 0, sizeof(dir));
|
|
|
|
memset(fname, 0, sizeof(fname));
|
|
|
|
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 );
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2010-09-22 21:43:42 +00:00
|
|
|
_tmakepath( LogFileName, drive, dir, fname, _T("leak.csv") );
|
2015-03-17 21:19:42 +00:00
|
|
|
|
2010-09-22 21:43:42 +00:00
|
|
|
HANDLE hLogFile = INVALID_HANDLE_VALUE;
|
2008-09-18 03:15:49 +00:00
|
|
|
do
|
|
|
|
{
|
2015-03-17 21:19:42 +00:00
|
|
|
hLogFile = CreateFile(
|
|
|
|
LogFileName,
|
|
|
|
GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
NULL,
|
|
|
|
CREATE_ALWAYS,
|
|
|
|
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
|
|
|
|
NULL
|
|
|
|
);
|
2008-09-18 03:15:49 +00:00
|
|
|
if (hLogFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2015-03-17 21:19:42 +00:00
|
|
|
if (GetLastError() == ERROR_SHARING_VIOLATION)
|
|
|
|
{
|
2010-09-22 21:43:42 +00:00
|
|
|
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);
|
2015-03-17 21:19:42 +00:00
|
|
|
if (Result == IDNO)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (hLogFile == INVALID_HANDLE_VALUE);
|
|
|
|
|
|
|
|
if (hLogFile != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
SetFilePointer(hLogFile,0,NULL,FILE_BEGIN);
|
|
|
|
|
2010-09-22 21:43:42 +00:00
|
|
|
DWORD dwWritten = 0;
|
2008-09-18 03:15:49 +00:00
|
|
|
char Msg[800];
|
|
|
|
_snprintf(Msg,sizeof(Msg),"Order, Source File, Line Number, Mem Size\r\n");
|
2010-09-22 21:43:42 +00:00
|
|
|
WriteFile( hLogFile,Msg,(DWORD)strlen(Msg),&dwWritten,NULL );
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
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);
|
2010-09-22 21:43:42 +00:00
|
|
|
WriteFile( hLogFile,Msg,(DWORD)strlen(Msg),&dwWritten,NULL );
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
CloseHandle(hLogFile);
|
|
|
|
}
|
2010-09-22 21:43:42 +00:00
|
|
|
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);
|
2015-03-17 21:19:42 +00:00
|
|
|
if (Result == IDYES)
|
|
|
|
{
|
2010-09-22 21:43:42 +00:00
|
|
|
ShellExecute(NULL,_T("open"),LogFileName,NULL,NULL,SW_SHOW);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CloseHandle(hSemaphone);
|
|
|
|
hSemaphone = NULL;
|
|
|
|
State = NotInitialized;
|
|
|
|
}
|
2015-03-17 21:19:42 +00:00
|
|
|
|
2010-09-22 21:43:42 +00:00
|
|
|
void * CMemList::AddItem ( size_t size, char * filename, int line)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
void *res = malloc(size);
|
2010-09-22 21:43:42 +00:00
|
|
|
if (res == NULL)
|
|
|
|
{
|
|
|
|
return res;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
2010-09-22 21:43:42 +00:00
|
|
|
RecordAddItem(res,size,filename,line);
|
|
|
|
return res;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2010-09-22 21:43:42 +00:00
|
|
|
void CMemList::RecordAddItem ( void * ptr, size_t size, const char * filename, int line)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
__try {
|
2015-03-17 21:19:42 +00:00
|
|
|
if (State == Initialized && hSemaphone != NULL)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
DWORD CurrentThread = GetCurrentThreadId();
|
|
|
|
DWORD Result = WaitForSingleObject(hSemaphone,CurrentThread != ThreadID ? 30000: 0);
|
2015-03-17 21:19:42 +00:00
|
|
|
if (Result != WAIT_TIMEOUT)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
ThreadID = CurrentThread;
|
|
|
|
|
|
|
|
DEBUG_LOCATION info;
|
|
|
|
strncpy(info.File,filename,sizeof(info.File));
|
|
|
|
info.line = line;
|
2010-09-22 21:43:42 +00:00
|
|
|
info.size = (int)size;
|
2008-09-18 03:15:49 +00:00
|
|
|
info.order = order++;
|
|
|
|
|
2010-09-22 21:43:42 +00:00
|
|
|
Insert(ptr, info);
|
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
long dwSemCount = 0;
|
2010-09-22 21:43:42 +00:00
|
|
|
ThreadID = (DWORD)-1;
|
2008-09-18 03:15:49 +00:00
|
|
|
ReleaseSemaphore(hSemaphone,1,&dwSemCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} __except (EXCEPTION_EXECUTE_HANDLER) {
|
2010-09-22 21:43:42 +00:00
|
|
|
//_asm int 3
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-22 21:43:42 +00:00
|
|
|
void CMemList::Insert(void *res, DEBUG_LOCATION &info)
|
|
|
|
{
|
|
|
|
MemList.insert(MEMLIST::value_type(res,info));
|
|
|
|
}
|
|
|
|
|
|
|
|
void * CMemList::ReAllocItem ( void * ptr, size_t size, const char * filename, int line)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-03-17 21:19:42 +00:00
|
|
|
void *res = realloc(ptr, size);
|
|
|
|
if (res == NULL)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2010-09-22 21:43:42 +00:00
|
|
|
return res;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
2015-03-17 21:19:42 +00:00
|
|
|
if (ptr != res)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
__try {
|
2015-03-17 21:19:42 +00:00
|
|
|
if (State == Initialized && hSemaphone != NULL)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
DWORD CurrentThread = GetCurrentThreadId();
|
|
|
|
DWORD Result = WaitForSingleObject(hSemaphone,CurrentThread != ThreadID ? 30000 : 0);
|
2015-03-17 21:19:42 +00:00
|
|
|
if (Result != WAIT_TIMEOUT)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
ThreadID = CurrentThread;
|
|
|
|
//Add new pointer
|
|
|
|
DEBUG_LOCATION info;
|
|
|
|
strncpy(info.File,filename,sizeof(info.File));
|
|
|
|
info.line = line;
|
2010-09-22 21:43:42 +00:00
|
|
|
info.size = (int)size;
|
2008-09-18 03:15:49 +00:00
|
|
|
info.order = order++;
|
|
|
|
|
2010-09-22 21:43:42 +00:00
|
|
|
Insert(res, info);
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//remove old pointer
|
2010-09-22 21:43:42 +00:00
|
|
|
Remove(ptr);
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
long dwSemCount = 0;
|
2010-09-22 21:43:42 +00:00
|
|
|
ThreadID = (DWORD)-1;
|
2008-09-18 03:15:49 +00:00
|
|
|
ReleaseSemaphore(hSemaphone,1,&dwSemCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} __except (EXCEPTION_EXECUTE_HANDLER) {
|
2010-09-22 21:43:42 +00:00
|
|
|
//_asm int 3
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2010-09-22 21:43:42 +00:00
|
|
|
void CMemList::Remove(void *ptr)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2010-09-22 21:43:42 +00:00
|
|
|
//remove old pointer
|
|
|
|
MEMLIST_ITER item = MemList.find(ptr);
|
2015-03-17 21:19:42 +00:00
|
|
|
if (item != MemList.end())
|
|
|
|
{
|
2010-09-22 21:43:42 +00:00
|
|
|
MemList.erase(ptr);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
2010-09-22 21:43:42 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 21:19:42 +00:00
|
|
|
void CMemList::removeItem (void * ptr, bool bFree )
|
2010-09-22 21:43:42 +00:00
|
|
|
{
|
2010-10-23 18:53:01 +00:00
|
|
|
if (bFree)
|
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
}
|
2015-03-17 21:19:42 +00:00
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
__try {
|
2015-03-17 21:19:42 +00:00
|
|
|
if (State == Initialized && hSemaphone != NULL)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
DWORD CurrentThread = GetCurrentThreadId();
|
|
|
|
DWORD Result = WaitForSingleObject(hSemaphone,CurrentThread != ThreadID ? 30000 : 0);
|
2015-03-17 21:19:42 +00:00
|
|
|
if (Result != WAIT_TIMEOUT)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
ThreadID = CurrentThread;
|
2010-09-22 21:43:42 +00:00
|
|
|
|
|
|
|
Remove(ptr);
|
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
long dwSemCount = 0;
|
2010-09-22 21:43:42 +00:00
|
|
|
ThreadID = (DWORD)-1;
|
2008-09-18 03:15:49 +00:00
|
|
|
ReleaseSemaphore(hSemaphone,1,&dwSemCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}__except(EXCEPTION_EXECUTE_HANDLER ){
|
2010-09-22 21:43:42 +00:00
|
|
|
//_asm int 3
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:19:42 +00:00
|
|
|
void MemTest_AddLeak(char* Comment)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
MemList()->AddItem(1,Comment,-1);
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:19:42 +00:00
|
|
|
void* MemTest_malloc (size_t size, char* filename, int line)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
return MemList()->AddItem(size,filename,line);
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:19:42 +00:00
|
|
|
void* MemTest_realloc (void* ptr, size_t size, char* filename, int line)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
return MemList()->ReAllocItem(ptr, size,filename,line);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2010-10-23 18:53:01 +00:00
|
|
|
MemList()->removeItem(ptr,true);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2010-09-22 21:43:42 +00:00
|
|
|
MemList()->RecordAddItem(ptr,dwSize,filename,line);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL MemTest_VirtualFree( LPVOID lpAddress, SIZE_T dwSize, DWORD dwFreeType )
|
|
|
|
{
|
|
|
|
if ((dwFreeType & MEM_RELEASE) != 0)
|
|
|
|
{
|
2010-10-23 18:53:01 +00:00
|
|
|
MemList()->removeItem(lpAddress,false);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
return VirtualFree(lpAddress,dwSize,dwFreeType);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2015-01-31 19:27:27 +00:00
|
|
|
#endif
|