c++ memory leak

Hi to all,can u tell me how to know memory leak occur in ur c++ programm in which line and how much without using any tool.
If you have a debugger, they can be quite useful, but without any tools it's just a matter of going through the code manually.
There are a few guidelines but no real hard and fast rules, there are so many ways to cause memory leaks. Things like a call to malloc() or new etc without a corresponding call to free() or delete etc. Other indications are things like pointers going out of scope, reassigning pointers and loosing what they were pointing to. It'e really down to experience to be able to spot problems.

As for how big the leak is, without tools or a debugger you're down to a pen and paper working out how many bytes are in the structures you're loosing, which is a bit of a waste of time when to be able do it, you must have found the leak and hopefully fixed it.

If it's the cost of tools that is the problem, then I'm sure someone will be able to point you to some free ones.
closed account (z05DSL3A)
If you are using Visual Studio or Visual C++ you can add the following to your project

1
2
3
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h> 


Once you have added the previous statements, you can dump memory leak information by including the following statement in your program:

_CrtDumpMemoryLeaks();

When you run your program under the debugger, _CrtDumpMemoryLeaks displays memory leak information in the Output window.

Detected memory leaks!
Dumping objects ->
C:\PROGRAM FILES\VISUAL STUDIO\MyProjects\leaktest\leaktest.cpp(20) : {18}
normal block at 0x00780E80, 64 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.


For more information see:
http://msdn.microsoft.com/en-us/library/e5ewb1h3(vs.80).aspx
Last edited on
You learn something every day. I'll have to remember that one.......not that my code ever has memory leaks of course :)
Topic archived. No new replies allowed.