In an MFC project I'm using an external library TTL that overloads operator new
. My problem is that I have a memory leak and I'd like to make use of the diagnostics given by DEBUG_NEW
. However I'm only able to compile this with DEBUG_NEW
commented-out else I get the error below.
The following demonstrates it. It is an MCVE when the Visual Studio project is made as a console application with MFC headers.
// The problem is when this is uncommented
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
struct HandleId
{
HandleId() {}
void* operator new (size_t t) {
return HandleId::operator new(t, 0, 0, 0);
};
void* operator new (size_t t, int, const char* file, int line) {
HandleId* tmp = (HandleId*)calloc(1, t);
return tmp;
};
void operator delete (void* v) {
free(v);
}
};
namespace hed
{
struct Node : public virtual HandleId
{
Node() {};
Node(double x, double y, double z = 0.0) {}
};
}
struct DtmNode : public hed::Node
{
DtmNode() {};
DtmNode(short pntRef, double x, double y, double z)
: hed::Node(x, y, z)
{};
};
int main()
{
DtmNode* pNode = new DtmNode(0, 1.0, 2.0, 3.0);
return 0;
}
With DEBUG_NEW
uncommented the above compilation fails (VS2019) with.
error C2661:
HandleId::operator new
: no overloaded function takes 3 arguments
Up to now I've lived with the macro commented-out. But I now do want it in debug builds for the obvious reason of helping catch memory leaks during debugging. DtmNode
is my class which I can change. HandleId
and the hed
namespace belong to the library.
My question:
- Is there a way to compile this with
DEBUG_NEW
uncommented? - Alternatively, is there any way to tell
DtmNode
not to use the overloadedoperator new
when in _DEBUG mode so that theDEBUG_NEW
is used as per "normal"?