Excuse me but wtf? What is this doing with DirectX 9.0a?
- Viewing Profile: Reputation: sasho648
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
Community Stats
- Group Members
- Active Posts 33
- Profile Views 1,034
- Member Title Member
- Age Age Unknown
- Birthday Birthday Unknown
-
Gender
Not Telling
137
Neutral
User Tools
Contacts
sasho648 hasn't added any contacts yet.
Latest Visitors
#5007681 DirectX 9.0a SDK
Posted by sasho648
on 06 December 2012 - 02:47 AM
#4978639 My first public solution
Posted by sasho648
on 10 September 2012 - 11:52 AM
So this actually is my first solution I made
. 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
MemoryHandler.h
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:
Here will print warning messages for invalid delete call (2) and a memory leak.
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.
- Home
- » Viewing Profile: Reputation: sasho648

Find content