maybe this will help you track down the leaks.
Code:
/*=============================================================================
memory_check.h
File description:
checks for memory leaks and writes to file any memory which isnt released via
the delete call
remarks:
this is not all my code it is derived from a post by Dion Picco on flipcode
http://www.flipcode.com/articles/art...oryleaks.shtml
many thanks
author:
=============================================================================*/
#ifndef _MEMORY_CHECK_H_
#define _MEMORY_CHECK_H_
#ifdef _DEBUG //just to make sure its only in debug versions and not release
///#include "stdafx.h"
#include <stdlib.h>
#include <malloc.h>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
__inline string cstring2string(char* c_string)
{
string message;
for(int i=0; i<=(int)strlen(c_string); i++)
message = message + c_string[i];
return message;
}
typedef struct
{
void* address;
unsigned int size;
string file;
int line;
} memory_info;
class Memory
{
public:
static Memory* get_manager();
~Memory();
void add(unsigned int size, char* file, int line, void* address);
void remove(void* address);
void save_to_file();
private:
ofstream out;
Memory();
static Memory* m_manager;
vector<memory_info> ptr_vector;
vector<memory_info> ::iterator ptr_iter;
};
Memory* Memory::m_manager = 0;
Memory::Memory()
{
;
}
Memory::~Memory()
{
delete m_manager;
}
Memory* Memory::get_manager()
{
if (m_manager == 0)
m_manager = new(Memory);
return m_manager;
}
void Memory::add(unsigned int size, char* file, int line, void* address)
{
memory_info info;
info.size = size;
info.file = cstring2string(file);
info.line = line;
info.address = address;
ptr_vector.push_back(info);
}
void Memory::remove(void* address)
{
for(ptr_iter = ptr_vector.begin(); ptr_iter != ptr_vector.end(); ++ptr_iter)
{
if(ptr_iter->address == address)
{
ptr_vector.erase(ptr_iter);
break;
}
}
}
void Memory::save_to_file()
{
char file_name[]="memory_leaks.txt";
out.open( file_name, ios::out /*| ios::app*/ );
if(!out.is_open())
{
cout <<"ERROR\ncould not open file " <<file_name <<endl;
}
else
{
for(ptr_iter = ptr_vector.begin(); ptr_iter != ptr_vector.end(); ++ptr_iter)
{
out <<"address:" <<ptr_iter->address <<" "
<<"size:" <<ptr_iter->size <<" "
<<"file:" <<ptr_iter->file <<" "
<<"line:" <<ptr_iter->line <<" " <<"\n";
}
out.close();
}
}
void* operator new(unsigned int size,char* file, int line)
{
void *ptr = (void *)malloc(size);
if(ptr == 0)
{
error("memory could not be allocated!");
}
Memory::get_manager()->add(size,file,line,ptr);
return ptr;
}
void operator delete(void* ptr)
{
Memory::get_manager()->remove(ptr);
//cant seem to use the destructot for the Memory class
//or check if the pointer is the memory ptr so I will just have to write it everytime ;(
//slow but what can I do??????????????
Memory::get_manager()->save_to_file();
free(ptr);
}
//some macro magic to make the new call give its line and file
#define DEBUG_NEW new(__FILE__, __LINE__)
#define new DEBUG_NEW
#endif //_DEBUG
#endif //_MEMORY_CHECK_H_
edit--
the error func referenced in this file is:
Code:
#define error(X) std::cout <<"Error: " <<X <<std::endl <<"\tfile: "<<__FILE__ <<std::endl << "\tline: "<<__LINE__ <<std::endl;
and this file doesnt check that a valid pointer is returned, its just for debug purposes.