I’m currently working on my own memory leak tracking system.
I’m using Microsoft Visual C++ 2008 and I know they have a built in one, but I’ve been making one for myself, just for the fun of it.
However, when I override the new and new[] commands, I get a function redefinition error, no matter what I do.
I don’t know much about Microsoft Visual C++’s internal bindings on things, but I’ve heard it is something with the CRT already defining the exact same macro I am.
I’ve seen articles on here and other places directed at this same exact question, but
people can never seem to solve the problem or never give a definite answer on how they
solved it.
Here is all the code I have up till this point:
MLD.h : http://pastebin.com/SfHzNaeN
MLD.cpp : http://pastebin.com/aRhsTZpv
All of the code is loosely based off a old flipcode article (How To Detect Memory Leaks).
Sorry I cannot give you a direct link, because I don’t have 10 rep to post more than 2 hyperlinks.
Thanks for your time.
The “function redefinition error” is probably because you’re using MFC.
The runtime library should not be in the business of defining such allocation and deallocation functions.
The current code’s
is ungood. An
unsigned intis not guaranteed to be large enough to hold an address, even though it is in 32-bit Windows. Instead, useintptr_t.Also, the current code’s
is ungood. That should be …
And you need a corresponding
operator delete, like …in order to deallocate memory from a failed constructor call. It’s only called in that very specific situation. But if you don’t have it, then you have a leak, as MFC once had for debug builds.
EDIT: fixed versions of the code you now provided:
file [minimal.h]:
_MINIMAL_His invalid, because it starts with underscore followed by uppercase letter, which is reserved. Changed toMINIMAL_H.size_tyou need to include<stddef.h>._DEBUGis not a standard macro. it’s a microsoftism. the standard macro (look up documentation ofassert) for this purpose isNDEBUG.file [minimal.cpp]:
mallocyou need to includestdlib.h.#define newyou’re wreaking havoc with thenewkeywords in the following code.malloc, and in C++ you should only cast something when there is a need for it. there is no such need here. casts only mask bugs, and that’s not a good idea.throw std::bad_alloc. that’s by the holy standard’s specification of these functions.