I'm finding the memory leak in my program, I tried the following guide from Microsoft
http://msdn.microsoft.com/en-us/library/e5ewb1h3%28v=vs.90%29.aspx
But there are some memory leak report items, don't have the source file name and line of code.
I tried to use VLD, but the VLD don't show the file name on call stack, although I did any step in the guides on internet.
From the memory leak report of VLD, I can see the size of block leaked. So I have the idea. Assume that I can overload the operator new, and debug on it, maybe I can detect where memory leak occurred by the special break point as code below.
void* operator new (size_t size)
{
if(size = 1107)
{
int temp = 0; //Put the break point here
}
void *p = malloc(size);
return p;
}
But I can't overload the operator new, because I an error when compiling the program:
Error 166 error LNK2005: "void * __cdecl operator new(unsigned __int64,char *,unsigned int)" (??2@YAPEAX_KPEADI@Z) already defined in CommonGlobal.obj E:\TIN HOC\Learning\Chuong Tring Dao Tao\Working\Main_Code\Mystic\branches\DVRServer\Common\Message.obj Common
Someone can tell me how to overload the operator new, for debugging in it?
The linker complains about redefinition of the operator new. You probably defined that function in a header file that is included in multiple files (at least in CommonGlobal.cpp and Message.cpp). Either move it to a source file or add an
inline
specifier to it. If you define the operator new is in source file, make sure it is in only one source file - it applies to all calls to the operator new in all of your source files.Also the
if(size = 1107)
is suspicious - it will always break and always allocates a memory of size 1107. Change it toif(size == 1107)
if you want to break when the size is equal to 1107. Under Windows you can also call__debugbreak()
which breaks into debugger without need to place a breakpoint manually, but you can't expect it to work under different complier than Visual C++.And for the Visual Leak Detector: did you see a message like
Visual Leak Detector Version 2.4RC2 installed
message in VS's output window and console? If not, switch active configuration from Release to Debug or#define VLD_FORCE_ENABLE
before inclusion of vld.h.