(CODE AT END OF POST)
I have got some code for finding memory leaks that works well with:
new()
and needs to be extended to work with
new[]
I have to use visual C++ 6.0 to compile the project. I know that <=5.0
did not support new[] overloading but I don't know about 6.0+. Perhaps
I am just doing something bad c++ wise:
When I add/uncomment my new[] code the compiler produces the following
error:
C:\PROGRA~1\MICROS~3\VC98\INCLUDE\xlocale(218) : error C2660: 'new[]'
: function does not take 1 par
ameters
C:\PROGRA~1\MICROS~3\VC98\INCLUDE\xlocnum(53) : see reference
to function template instantia
tion 'char *__cdecl std::_Maklocstr(const char *,char *)' being
compiled
C:\PROGRA~1\MICROS~3\VC98\INCLUDE\xlocale(218) : error C2660: 'new[]'
: function does not take 1 par
ameters
C:\PROGRA~1\MICROS~3\VC98\INCLUDE\xlocnum(54) : see reference
to function template instantia
tion 'unsigned short *__cdecl std::_Maklocstr(const char *,unsigned
short *)' being compiled
NMAKE : fatal error U1077: 'cl.exe' : return code '0x2'
Stop.
The offending line in xlocale:
_E *_X = new _E[_L];
The offending code in xlocnum:
_Gr = _MAKLOCSTR(char, _P->grouping);
_Nf = _MAKLOCSTR(_E, _Lobj._Getfalse());
My header file:
#ifndef MEMORY_LEAK_FINDER_H
#define MEMORY_LEAK_FINDER_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <list>
#include <iostream.h>
//using namespace std;
// Tracing Utility
typedef unsigned long DWORD;
typedef struct {
DWORD address;
DWORD size;
char fileName[64];
DWORD lineNumber;
} ALLOCATED_INFO;
typedef std::list<ALLOCATED_INFO*> AllocatedList;
void addTrace(DWORD addr, DWORD asize, const char *fname, DWORD
lnum);
void removeTrace(DWORD addr);
extern AllocatedList *allocatedList;
void Dump();
//void * __cdecl operator new(size_t size,const char *fileName, int
lineNumber);
//void __cdecl operator delete(void *p,const char *fileName, int
lineNumber);
//void __cdecl operator delete(void *p);
inline void * operator new(size_t size,const char *fileName, int
lineNumber) {
void *ptr = (void *)malloc(size);
addTrace((DWORD)ptr, size, fileName, lineNumber);
return(ptr);
};
/*void * operator new[](size_t size,const char *fileName, int
lineNumber) {
void *ptr = (void *)malloc(size);
addTrace((DWORD)ptr, size, fileName, lineNumber);
return(ptr);
};*/
inline void operator delete(void *p,const char *fileName, int
lineNumber) {
removeTrace((DWORD)p);
free(p);
};
/*void operator delete[](void *p,const char *fileName, int
lineNumber) {
removeTrace((DWORD)p);
free(p);
}; */
inline void operator delete(void *p) {
removeTrace((DWORD)p);
free(p);
};
/*void operator delete[](void *p) {
removeTrace((DWORD)p);
free(p);
}; */
#ifdef _DEBUG
#define debugnew new(__FILE__, __LINE__)
#else
#define debugnew new
#endif
#endif