Hi Friends,

Can any tell me a free tool which detects memory leak in C++ code.
to be more specific [ UNMANAGED C++ code ]. i am using some C Structs in my
MFC SDI application, When i run in debug mode , the output window shows that memory has leaked detected but i dont exactly know where the leak
is happening can any one tell me a tool which can detect the leak?
i am using VS2005 , do i have any option of finding the leak using VS2005?
Thanks in Advance.


- We can gain experience by doing same things repeatedly , but innovation comes only by doing different things ,To Innovate we need experience..... :|
- KoushalVV

use this snippet in your code:

#include <crtdbg.h>

int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
return 0;
}

This won't actually detect where the leak is occuring, but can give you a good idea. For example, if you are allocating 20 bytes of memory for a certain array, and the output window says it found a memory leak 20 bytes long, you should have a good idea which array you forgot to delete. The best way to prevent memory leaks is to have a delete line for every new line.

Thanks for the reply,
but problem is the code size is too huge
its around 10k lines of code, the problem in
the code is dangling pointers [not sure ]. i have a delete operator
for every new operator tat i have created , but the problem
is i will be sending pointers to other class [performing a deep copy ] and
returning the pointers back , deleting the pointers in the same class
tat i have created. since i have done a deep copy will this create
a dangling pointer???
how ever i will try the snippet given by u , any tool which can say
tat this particular line has memory leak , like we have NCOVER
tool for C#,C++ managed code. i am not able to find one for unmanaged

To add to what skatamatic skatamatic said.

The leak report also prints out a {nnnnn} number, which is the number of the block allocated. There is a variable in crtdbg.h which you can set to this number the next time you run the program (in the debugger).

When that block is allocated, it will trap into the debugger to give you a nice opportunity to look around at who is allocating it, and therefor who should be freeing it later.

Well if do you know if the memory leak is caused by a recent addition to the code, or has there been memory leaks from the start? You should be checking for mem leaks as you write the code, not at the end >.<

commented: Agreed, and add lint to the list as well. +22

yeah i am one lazy developer :(
thanks for the reply guys i will surely try out this solution
and get back to u .....

> You should be checking for mem leaks as you write the code, not at the end
Agreed, and add lint to the list as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.