2015-12-23 02:10:04 +00:00
|
|
|
#include "stdafx.h"
|
2018-11-25 21:00:50 +00:00
|
|
|
#include "MemTest.h"
|
|
|
|
|
|
|
|
#if defined(MEM_LEAK_TEST) && defined(_WIN32)
|
|
|
|
|
2018-11-18 01:56:00 +00:00
|
|
|
#undef new
|
2015-12-23 02:10:04 +00:00
|
|
|
|
2018-11-25 21:00:50 +00:00
|
|
|
#include <Windows.h>
|
2018-11-18 01:56:00 +00:00
|
|
|
#include <shellapi.h>
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2018-11-18 01:56:00 +00:00
|
|
|
static bool InInit = false;
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2018-11-18 01:56:00 +00:00
|
|
|
class CMemList
|
|
|
|
{
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char File[300];
|
|
|
|
int line;
|
|
|
|
int size;
|
|
|
|
int order;
|
|
|
|
} DEBUG_LOCATION;
|
|
|
|
|
|
|
|
typedef std::map<void *, DEBUG_LOCATION> MEMLIST;
|
|
|
|
typedef MEMLIST::const_iterator MEMLIST_ITER;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CMemList();
|
|
|
|
~CMemList();
|
|
|
|
|
|
|
|
void removeItem(void * ptr);
|
|
|
|
void RecordAddItem(void * ptr, size_t size, const char * filename, int line);
|
|
|
|
void DumpItems(void);
|
|
|
|
|
|
|
|
private:
|
|
|
|
MEMLIST * m_MemList;
|
|
|
|
HMODULE m_hModule;
|
|
|
|
CRITICAL_SECTION m_cs;
|
|
|
|
uint32_t m_NextOrder;
|
|
|
|
};
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-12-23 02:10:04 +00:00
|
|
|
CMemList *MemList(void)
|
2015-03-17 21:19:42 +00:00
|
|
|
{
|
2015-12-23 02:10:04 +00:00
|
|
|
static CMemList m_MemList;
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-12-23 02:10:04 +00:00
|
|
|
return &m_MemList;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2018-11-18 01:56:00 +00:00
|
|
|
CMemList::CMemList() :
|
|
|
|
m_MemList(NULL),
|
|
|
|
m_hModule(NULL),
|
|
|
|
m_cs({0}),
|
|
|
|
m_NextOrder(1)
|
2015-03-17 21:19:42 +00:00
|
|
|
{
|
2018-11-18 01:56:00 +00:00
|
|
|
InInit = true;
|
|
|
|
InitializeCriticalSection(&m_cs);
|
|
|
|
m_MemList = new MEMLIST;
|
2015-12-23 02:10:04 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2018-11-18 01:56:00 +00:00
|
|
|
InInit = false;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 21:19:42 +00:00
|
|
|
CMemList::~CMemList()
|
|
|
|
{
|
2018-11-18 01:56:00 +00:00
|
|
|
InInit = true;
|
|
|
|
size_t ItemsLeft = m_MemList->size();
|
2015-12-23 02:10:04 +00:00
|
|
|
if (ItemsLeft > 0)
|
|
|
|
{
|
2018-11-18 01:56:00 +00:00
|
|
|
DumpItems();
|
2015-12-23 02:10:04 +00:00
|
|
|
}
|
2018-11-18 01:56:00 +00:00
|
|
|
delete m_MemList;
|
|
|
|
m_MemList = NULL;
|
|
|
|
DeleteCriticalSection(&m_cs);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
2015-03-17 21:19:42 +00:00
|
|
|
|
2018-11-18 01:56:00 +00:00
|
|
|
void CMemList::RecordAddItem(void * ptr, size_t size, const char * filename, int line)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2018-11-18 01:56:00 +00:00
|
|
|
EnterCriticalSection(&m_cs);
|
|
|
|
if (m_cs.RecursionCount > 1)
|
2015-12-23 02:10:04 +00:00
|
|
|
{
|
2018-11-18 01:56:00 +00:00
|
|
|
LeaveCriticalSection(&m_cs);
|
|
|
|
return;
|
2015-12-23 02:10:04 +00:00
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2018-11-18 01:56:00 +00:00
|
|
|
DEBUG_LOCATION info;
|
|
|
|
strncpy(info.File, filename, sizeof(info.File));
|
|
|
|
info.line = line;
|
|
|
|
info.size = (int)size;
|
|
|
|
info.order = m_NextOrder++;
|
|
|
|
m_MemList->insert(MEMLIST::value_type(ptr, info));
|
|
|
|
LeaveCriticalSection(&m_cs);
|
|
|
|
}
|
2015-12-23 02:10:04 +00:00
|
|
|
|
2018-11-18 01:56:00 +00:00
|
|
|
void CMemList::removeItem(void * ptr)
|
|
|
|
{
|
|
|
|
EnterCriticalSection(&m_cs);
|
|
|
|
if (m_cs.RecursionCount > 1)
|
|
|
|
{
|
|
|
|
LeaveCriticalSection(&m_cs);
|
|
|
|
return;
|
2015-12-23 02:10:04 +00:00
|
|
|
}
|
2018-11-18 01:56:00 +00:00
|
|
|
MEMLIST_ITER item = m_MemList->find(ptr);
|
|
|
|
if (item != m_MemList->end())
|
2015-12-23 02:10:04 +00:00
|
|
|
{
|
2018-11-18 01:56:00 +00:00
|
|
|
m_MemList->erase(item);
|
2015-12-23 02:10:04 +00:00
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2018-11-18 01:56:00 +00:00
|
|
|
LeaveCriticalSection(&m_cs);
|
2010-09-22 21:43:42 +00:00
|
|
|
}
|
|
|
|
|
2018-11-18 01:56:00 +00:00
|
|
|
void CMemList::DumpItems(void)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2018-11-18 01:56:00 +00:00
|
|
|
char path_buffer[_MAX_PATH] = { 0 }, drive[_MAX_DRIVE] = { 0 }, dir[_MAX_DIR] = { 0 };
|
|
|
|
char fname[_MAX_FNAME] = { 0 }, ext[_MAX_EXT] = { 0 }, LogFileName[_MAX_PATH] = { 0 };
|
|
|
|
|
2020-06-09 13:04:38 +00:00
|
|
|
GetModuleFileNameA(m_hModule, path_buffer, sizeof(path_buffer));
|
2018-11-18 01:56:00 +00:00
|
|
|
_splitpath(path_buffer, drive, dir, fname, ext);
|
|
|
|
_makepath(LogFileName, drive, dir, fname, "leak.csv");
|
|
|
|
|
|
|
|
HANDLE hLogFile = INVALID_HANDLE_VALUE;
|
|
|
|
do
|
2015-12-23 02:10:04 +00:00
|
|
|
{
|
2020-06-09 13:04:38 +00:00
|
|
|
hLogFile = CreateFileA( LogFileName, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL );
|
2018-11-18 01:56:00 +00:00
|
|
|
if (hLogFile == INVALID_HANDLE_VALUE)
|
2015-12-23 02:10:04 +00:00
|
|
|
{
|
2018-11-18 01:56:00 +00:00
|
|
|
if (GetLastError() == ERROR_SHARING_VIOLATION)
|
2015-12-23 02:10:04 +00:00
|
|
|
{
|
2020-06-09 13:04:38 +00:00
|
|
|
char Msg[3000];
|
2018-11-18 01:56:00 +00:00
|
|
|
sprintf(Msg, "%s\nCan not be opened for writing please close app using this file\n\nTry Again ?", LogFileName);
|
2020-06-09 13:04:38 +00:00
|
|
|
int Result = MessageBoxA(NULL, Msg, "Memory Leak", MB_YESNO | MB_ICONQUESTION | MB_SETFOREGROUND | MB_SERVICE_NOTIFICATION);
|
2018-11-18 01:56:00 +00:00
|
|
|
if (Result == IDNO)
|
2015-12-23 02:10:04 +00:00
|
|
|
{
|
2018-11-18 01:56:00 +00:00
|
|
|
break;
|
2015-12-23 02:10:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-18 01:56:00 +00:00
|
|
|
} while (hLogFile == INVALID_HANDLE_VALUE);
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2018-11-18 01:56:00 +00:00
|
|
|
if (hLogFile != INVALID_HANDLE_VALUE)
|
2015-12-23 02:10:04 +00:00
|
|
|
{
|
2018-11-18 01:56:00 +00:00
|
|
|
SetFilePointer(hLogFile, 0, NULL, FILE_BEGIN);
|
2010-09-22 21:43:42 +00:00
|
|
|
|
2018-11-18 01:56:00 +00:00
|
|
|
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);
|
2015-12-23 02:10:04 +00:00
|
|
|
|
2018-11-18 01:56:00 +00:00
|
|
|
for (MEMLIST_ITER item = m_MemList->begin(); item != m_MemList->end(); item++)
|
2015-12-23 02:10:04 +00:00
|
|
|
{
|
2018-11-18 01:56:00 +00:00
|
|
|
_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);
|
2015-12-23 02:10:04 +00:00
|
|
|
}
|
2018-11-18 01:56:00 +00:00
|
|
|
CloseHandle(hLogFile);
|
2015-12-23 02:10:04 +00:00
|
|
|
}
|
2018-11-18 01:56:00 +00:00
|
|
|
char Msg[3000];
|
|
|
|
sprintf(Msg, "%s%s\n\nMemory Leaks detected\n\nOpen the Log File ?", fname, ext);
|
2020-06-09 13:04:38 +00:00
|
|
|
int Result = MessageBoxA(NULL, Msg, "Memory Leak", MB_YESNO | MB_ICONQUESTION | MB_SETFOREGROUND | MB_SERVICE_NOTIFICATION);
|
2018-11-18 01:56:00 +00:00
|
|
|
if (Result == IDYES)
|
2015-12-23 02:10:04 +00:00
|
|
|
{
|
2020-06-09 13:04:38 +00:00
|
|
|
ShellExecuteA(NULL, "open", LogFileName, NULL, NULL, SW_SHOW);
|
2015-12-23 02:10:04 +00:00
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2018-11-18 01:56:00 +00:00
|
|
|
void* AllocateMemory(size_t size, const char* filename, unsigned int line)
|
2015-03-17 21:19:42 +00:00
|
|
|
{
|
2018-11-18 01:56:00 +00:00
|
|
|
void * res = malloc(size);
|
|
|
|
if (res == NULL)
|
|
|
|
{
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
if (!InInit)
|
|
|
|
{
|
|
|
|
MemList()->RecordAddItem(res, size, filename, line);
|
|
|
|
}
|
|
|
|
return res;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2018-11-18 01:56:00 +00:00
|
|
|
void* operator new (size_t size, const char* filename, unsigned int line)
|
2015-03-17 21:19:42 +00:00
|
|
|
{
|
2018-11-18 01:56:00 +00:00
|
|
|
return AllocateMemory(size, filename, line);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2018-11-18 01:56:00 +00:00
|
|
|
void* operator new[] (size_t size, const char* filename, unsigned int line)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2018-11-18 01:56:00 +00:00
|
|
|
return AllocateMemory(size, filename, line);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void* operator new (size_t size)
|
|
|
|
{
|
2018-11-18 01:56:00 +00:00
|
|
|
return AllocateMemory(size, "Unknown", 0);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-12-23 02:10:04 +00:00
|
|
|
void operator delete (void* ptr)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2018-11-18 01:56:00 +00:00
|
|
|
free(ptr);
|
|
|
|
if (!InInit)
|
|
|
|
{
|
|
|
|
MemList()->removeItem(ptr);
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void operator delete[](void* ptr)
|
|
|
|
{
|
2015-12-23 02:10:04 +00:00
|
|
|
delete ptr;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2018-11-18 01:56:00 +00:00
|
|
|
void operator delete (void* ptr, const char* /*filename*/, unsigned int /*line*/)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2018-11-18 01:56:00 +00:00
|
|
|
delete ptr;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2018-11-18 01:56:00 +00:00
|
|
|
void operator delete[](void* ptr, const char* /*filename*/, unsigned int /*line*/)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2018-11-18 01:56:00 +00:00
|
|
|
delete ptr;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2018-11-25 21:00:50 +00:00
|
|
|
#endif
|