MemoryHandler.cpp
#include <iostream>
#include <windows.h>
#include <stdio.h>
#ifdef DEBUG
struct MemoryAllocTree {
char* File;
unsigned int Line;
MemoryAllocTree* next;
void* MemoryPointer;
} * _gpAllocTree(0) ;
MemoryAllocTree** ppCurAllocTree(&_gpAllocTree);
unsigned int _gCurLine;
char* _gCurFile;
#endif
void* operator new(size_t size)
{
void* pMem(malloc(size ? size : 1));
if(!pMem)
{
#ifdef WIN32_MEM_LOG
MessageBoxA(0, "No more memory to use. The app will exit now!", "Error!", MB_OK | MB_ICONERROR);
#endif
#ifndef WIN32_MEM_LOG
std::cout << "No more memory to use. The app will exit now!" << "Error!" << std::endl;
#endif
exit(1);
}
#ifdef DEBUG
(*ppCurAllocTree)=(MemoryAllocTree*)malloc(sizeof(MemoryAllocTree));
if(!(*ppCurAllocTree))
{
#ifdef WIN32_MEM_LOG
MessageBoxA(0, "No more memory to use for the debug version of MemoryHandler. Try compiling the app \
in release mode. It will exit now!", "Error!", MB_OK | MB_ICONERROR);
#endif
#ifndef WIN32_MEM_LOG
std::cout << "No more memory to use for the debug version of MemoryHandler. Try compiling the app \
in release mode. It will exit now!" << "Error!" << std::endl;
#endif
exit(1);
}
(*ppCurAllocTree)->Line=_gCurLine;
(*ppCurAllocTree)->File=_gCurFile;
(*ppCurAllocTree)->MemoryPointer=pMem;
(*ppCurAllocTree)->next=0;
ppCurAllocTree=&(*ppCurAllocTree)->next;
#endif
return pMem;
}
void* operator new[](size_t size)
{
return operator new(size);
}
void operator delete[](void* pMem)
{
operator delete(pMem);
}
void operator delete(void* pMem)
{
if(!pMem) return;
#ifdef DEBUG
MemoryAllocTree* sourc(_gpAllocTree);
while(sourc&&sourc->MemoryPointer!=pMem)
sourc=sourc->next;
if(!sourc)
{
#ifdef DEBUG
#ifdef WIN32_MEM_LOG
char str[80];
sprintf(str, "Invalid delete call at File: %s Line: %d", _gCurFile,
_gCurLine);
MessageBoxA(0, str, "Warning!", MB_OK | MB_ICONWARNING);
#endif
#ifndef WIN32_MEM_LOG
std::cout << "Invalid delete call at File: " << _gCurFile << " Line: " << _gCurLine << " Warning!" << std::endl;
#endif
#endif
return;
}
sourc->MemoryPointer=0;
#endif
free(pMem);
return;
}
#ifdef DEBUG
void EndOfProgramMemCheck() {
MemoryAllocTree* pCurAllocTree(_gpAllocTree);
while(pCurAllocTree) {
if(pCurAllocTree->MemoryPointer) {
#ifndef WIN32_MEM_LOG
std::cout << "Object at line: " << pCurAllocTree->Line << " in file: "
<< pCurAllocTree->File << " wasn't deleted!" << std::endl;
#endif
#ifdef WIN32_MEM_LOG
char str[80];
sprintf(str, "Object at line: %d in file: %s wasn't deleted!", pCurAllocTree->Line,
pCurAllocTree->File);
MessageBoxA(0, str, "Memory leaked!", MB_OK | MB_ICONWARNING);
#endif
}
MemoryAllocTree* cach(pCurAllocTree);
pCurAllocTree=pCurAllocTree->next;
free(cach);
}
}
#endifMemoryHandler.h
#pragma once /*Sasho 648 Memory Handle functions and memory leaks finder - define DEBUG to active it and add at the function EndOfProgramMemCheck() before exit your app. Define a WIN32_MEM_LOG to active an Win32 Messages. Type MEM_FUNC_CALL at the line begin where new\delete \[] function is called. Example: MEM_FUNC_CALL char* ptr = new char[50]; */ #ifdef DEBUG extern unsigned int _gCurLine; extern char* _gCurFile; #endif #ifdef DEBUG #define MEM_FUNC_CALL _gCurLine=__LINE__; _gCurFile=(char*)__FILE__; #endif #ifndef DEBUG #define MEM_FUNC_CALL #endif void* operator new(size_t size) ; void* operator new[](size_t size) ; void operator delete[](void* pMem) ; void operator delete(void* pMem) ; #ifdef DEBUG void EndOfProgramMemCheck() ; #endif
You need to make compiling debug define (DEBUG) at the compiler options and for win32 message posting you need to define WIN32_MEM_LOG. Before each new\delete call you need to type MEM_FUNC_CALL.
Here is a simple code using my solution:
class My {
public:
My() {std::cout<<"My::My()"<<std::endl;}
~My() {std::cout<<"My::~My()"<<std::endl;}
} ;
int main() {
MEM_FUNC_CALL My* ptr=new My[23];
MEM_FUNC_CALL delete[] ptr;
MEM_FUNC_CALL delete ptr;
size_t* My_Size_T=new size_t;
My_Size_T=(size_t*)0xbff7ffff;
delete My_Size_T;
#ifdef DEBUG
EndOfProgramMemCheck();
#endif
system("PAUSE");
return 0;
}Here will print warning messages for invalid delete call (2) and a memory leak.
Edited by sasho648, 10 September 2012 - 11:58 AM.







