Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#Actualsasho648

Posted 10 September 2012 - 11:58 AM

So this actually is my first solution I made Posted Image . It is c++ new\delete memory leaks finder. It does an memory check at the end of the program and if some object created with new wasn't deleted it post a message with the object line and source file name. It use an a tree for collecting runtime memory actions. Here is the .cpp and .h file. I will be happy for any opinion and advice about this solution.

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);
	}
}
#endif



MemoryHandler.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.

#6sasho648

Posted 10 September 2012 - 11:57 AM

So this actually is my first solution I made Posted Image . It is c++ new\delete memory leaks finder. It does an memory check at the end of the program and if some object created with new wasn't deleted it post a message with the object line and source file name. It use an a tree for collecting runtime memory actions. Here is the .cpp and .h file. I will be happy for any opinion and advice about this solution.

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);
	}
}
#endif



MemoryHandler.h

[source lang="cpp"]#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 DEBUGextern 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#endifvoid* operator new(size_t size) ;void* operator new[](size_t size) ;void operator delete[](void* pMem) ;void operator delete(void* pMem) ;#ifdef DEBUGvoid EndOfProgramMemCheck() ;#endif[/source]


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:

[source lang="cpp"]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;}[/source]


Here will print warning messages for invalid delete call (2) and a memory leak.

#5sasho648

Posted 10 September 2012 - 11:56 AM

So this actually is my first solution I made Posted Image . It is c++ new\delete memory leaks finder. It does an memory check at the end of the program and if some object created with new wasn't deleted it post a message with the object line and source file name. It use an a tree for collecting runtime memory actions. Here is the .cpp and .h file. I will be happy for any opinion and advice about this solution.

MemoryHandler.cpp

[source lang="cpp"]#include <iostream>#include <windows.h>#include <stdio.h>#ifdef DEBUGstruct MemoryAllocTree {    char* File;    unsigned int Line;    MemoryAllocTree* next;    void* MemoryPointer;} * _gpAllocTree(0) ;MemoryAllocTree** ppCurAllocTree(&_gpAllocTree);unsigned int _gCurLine;char* _gCurFile;#endifvoid* 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 DEBUGvoid 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);    }}#endif[/source]


MemoryHandler.h

[source lang="cpp"]#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 DEBUGextern 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#endifvoid* operator new(size_t size) ;void* operator new[](size_t size) ;void operator delete[](void* pMem) ;void operator delete(void* pMem) ;#ifdef DEBUGvoid EndOfProgramMemCheck() ;#endif[/source]


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:

[source lang="cpp"]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;}[/source]


Here will print warning messages for invalid delete call (2) and a memory leak.

#4sasho648

Posted 10 September 2012 - 11:54 AM

So this actually is my first solution I made Posted Image . It is c++ new\delete memory leaks finder. It does an memory check at the end of the program and if some object created with new wasn't deleted it post a message with the object line and source file name. It use an a tree for collecting runtime memory actions. Here is the .cpp and .h file. I will be happy for any opinion and advice about this solution.

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);
}
}
#endif

MemoryHandler.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.

#3sasho648

Posted 10 September 2012 - 11:53 AM

So this actually is my first solution I made Posted Image . It is c++ new\delete memory leaks finder. It does an memory check at the end of the program and if some object created with new wasn't deleted it post a message with the object line and source file name. It use an a tree for collecting runtime memory actions. Here is the .cpp and .h file. I will be happy for any opinion and advice about this solution.

MemoryHandler.cpp
[source lang="cpp"]#include <iostream>#include <windows.h>#include <stdio.h>#ifdef DEBUGstruct MemoryAllocTree { char* File; unsigned int Line; MemoryAllocTree* next; void* MemoryPointer;} * _gpAllocTree(0) ;MemoryAllocTree** ppCurAllocTree(&_gpAllocTree);unsigned int _gCurLine;char* _gCurFile;#endifvoid* 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 DEBUGvoid 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); }}#endif[/source]
MemoryHandler.h
[source lang="cpp"]#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 DEBUGextern 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#endifvoid* operator new(size_t size) ;void* operator new[](size_t size) ;void operator delete[](void* pMem) ;void operator delete(void* pMem) ;#ifdef DEBUGvoid EndOfProgramMemCheck() ;#endif[/source]

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.

#2sasho648

Posted 10 September 2012 - 11:52 AM

So this actually is my first solution I made Posted Image . It is c++ new\delete memory leaks finder. It does an memory check at the end of the program and if some object created with new wasn't deleted it post a message with the object line and source file name. It use an a tree for collecting runtime memory actions. Here is the .cpp and .h file. I will be happy for any opinion and advice about this solution.

MemoryHandler.cpp
[source lang="cpp"]#include <iostream>#include <windows.h>#include <stdio.h>#ifdef DEBUGstruct MemoryAllocTree { char* File; unsigned int Line; MemoryAllocTree* next; void* MemoryPointer;} * _gpAllocTree(0) ;MemoryAllocTree** ppCurAllocTree(&_gpAllocTree);unsigned int _gCurLine;char* _gCurFile;#endifvoid* 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 DEBUGvoid 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); }}#endif[/source]
MemoryHandler.h
[source lang="cpp"]#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 DEBUGextern 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#endifvoid* operator new(size_t size) ;void* operator new[](size_t size) ;void operator delete[](void* pMem) ;void operator delete(void* pMem) ;#ifdef DEBUGvoid EndOfProgramMemCheck() ;#endif[/source]

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:

[source lang="cpp"]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;}[/source]

Here will print warning messages for invalid delete call (2) and a memory leak.

PARTNERS