hi, i've been trying to create memory debugger, but i'm struggling to get my overloaded operator new to compile. my code is this:
main.cpp
#include "leaky.h"
int main()
{
int num = new int; // Create a leak
ReportLeaks(); // Report the leak
};
leaky.cpp
#include "leaky.h"
#undef MEM_DEBUG
list<mem_info> tracker;
void * operator new(size_t size, const char *file, int line)
{
try
{
// Allocate the memory check that the allocation was a success and add to the tracker
if(size == 0)
{
size = 1; // Make sure a size of 0 is not sent to malloc
}
void *ptr = (void *)malloc(size);
if(ptr == 0)
{
throw bad_alloc();
}
mem_info m_info(ptr, size, file, line);
AddTracker(m_info);
return(ptr);
}
catch(bad_alloc)
{
// Handle an error
fstream mem("leaks.txt");
mem << "MEMORY ALLOCATION ERROR" << "\n";
}
};
void operator delete(void *p)
{
RemoveTracker((void *)p);
free(p);
};
void AddTracker(mem_info minfo)
{
tracker.push_back(minfo);
};
void RemoveTracker(void *ptr)
{
list<mem_info>::iterator iter;
for(iter = tracker.begin(); iter != tracker.end(); iter++)
{
if((*iter).GetPointer() == ptr)
{
tracker.remove((*iter));
break;
}
}
};
void ReportLeaks()
{
list<mem_info>::iterator iter;
fstream mem("leaks.txt");
// Report the memory leaks
mem << "\tAddress\t" << "\tSize\t" << "\tFile\t" << "\tLine\t" << "\n\n";
mem << "\t----------" << "----------" << "----------" << "----------" << "\n\n";
for(iter = tracker.begin(); iter != tracker.end(); iter++)
{
mem << "\t" << (*iter).GetPointer() << "\t\t" << (*iter).GetSize() << "\t\t"
<< (*iter).GetFile() << "\t\t" << (*iter).GetLine() << "\t\n\n";
}
for(iter = tracker.begin(); iter != tracker.end(); iter++)
{
free((*iter).GetPointer()); // Make sure that no memory is left allocated
}
mem.close();
};
// return file name
const char* mem_info::GetFile()
{
return file;
};
// return line number
int mem_info::GetLine()
{
return line;
};
// return address of object
void * mem_info::GetPointer()
{
return ptr;
};
// return size of object
size_t mem_info::GetSize()
{
return size;
};
bool mem_info::operator ==(const mem_info &other) const
{
return this->ptr == other.ptr;
}
#define MEM_DEBUG
leaky.h
/////////////////////////////////////////////////////////////////////
// Detect memory leaks //
/////////////////////////////////////////////////////////////////////
#include <cstddef> // For size_t
#include <list> // For list
#include <stdlib.h> // For malloc and free
#include <stdexcept> // For bad_alloc
#include <fstream> // For file access
using std::size_t;
using std::bad_alloc;
using std::list;
using std::fstream;
// replace the default new with new modified version
#ifdef MEM_DEBUG
#define new new(__FILE__, __LINE__)
#endif
#undef MEM_DEBUG
void* operator new(size_t size, const char *file, int line);
void operator delete(void *p);
class mem_info
{
public:
mem_info(void *mptr, size_t msize, const char* mfile, int mline)
: ptr(mptr), size(msize), file(mfile), line(mline) {}; // Set up the class
~mem_info() {}; // Cleanup the class
void * GetPointer(); // Get the address of the object
size_t GetSize(); // Get the size of the object
const char* GetFile(); // Get the file name
int GetLine(); // Get the line number
bool operator==( const mem_info &other ) const;
private:
void *ptr; // The address of the allocated memory
size_t size; // The size on the memory allocated
const char* file; // The file in which the allocation accured
int line; // The line in which the allocation accured
};
void AddTracker(mem_info minfo); // Add the allocated memory info to the list
void RemoveTracker(void *ptr); // Remove the allocated memory info from the list
void ReportLeaks(); // Report the leaks
#define MEM_DEBUG
and the errors are:
------ Build started: Project: leaky, Configuration: Debug Win32 ------
Compiling...
leaky.cpp
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2365: 'operator new' : redefinition; previous definition was 'function'
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2078: too many initializers
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2440: 'initializing' : cannot convert from 'int' to 'void *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2143: syntax error : missing ';' before '('
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2226: syntax error : unexpected type 'size_t'
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2059: syntax error : ')'
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(6) : error C2365: 'operator new' : redefinition; previous definition was 'function'
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(6) : error C2078: too many initializers
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(6) : error C2440: 'initializing' : cannot convert from 'int' to 'void *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(6) : error C2143: syntax error : missing ';' before '('
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(6) : error C2226: syntax error : unexpected type 'size_t'
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(6) : error C2059: syntax error : ')'
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(7) : error C2143: syntax error : missing ';' before '{'
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.cpp(7) : error C2447: '{' : missing function header (old-style formal list?)
main.cpp
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2365: 'operator new' : redefinition; previous definition was 'function'
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2078: too many initializers
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2440: 'initializing' : cannot convert from 'int' to 'void *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2143: syntax error : missing ';' before '('
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2226: syntax error : unexpected type 'size_t'
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\leaky.h(21) : error C2059: syntax error : ')'
c:\documents and settings\jason\my documents\visual studio 2005\projects\leaky\leaky\main.cpp(5) : error C2661: 'operator new' : no overloaded function takes 3 arguments
Generating Code...
Build Time 0:01
Build log was saved at "file://c:\Documents and Settings\Jason\My Documents\Visual Studio 2005\Projects\leaky\leaky\Debug\BuildLog.htm"
leaky - 21 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
btw i'mn using mscv 2005 and am defining MEM_DEBUG in project settings
thanks jason