Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find memory leaks in source code

If it is known that an application leaks memory (when executed), what are the various ways to locate such memory leak bugs in the source code of the application.
I know of certain parsers/tools (which probably do static analysis of the code) which can be used here but are there any other ways/techniques to do that, specific to the language (C/C++)/platform?

like image 939
TL36 Avatar asked Dec 14 '09 05:12

TL36


People also ask

How do you find memory leaks in code?

The primary tools for detecting memory leaks are the C/C++ debugger and the C Run-time Library (CRT) debug heap functions. The #define statement maps a base version of the CRT heap functions to the corresponding debug version. If you leave out the #define statement, the memory leak dump will be less detailed.

How do you check for memory leaks in Visual Studio code?

It seems that you can find memory leaks in VS Code C++ application with MSVC by simply adding the compiler option "/MDd" or "/MTd" in the args array of the tasks. json file within the project's . vscode folder (without any 3rd party application or tool).

What is memory leak in coding?

A memory leak is a program error that consists of repeatedly allocating memory, using it, and then neglecting to free it.


1 Answers

Purify will do a seemingly miraculous job of doing this

Not only memory leaks, but many other kinds of memory errors.

It works by instrumenting your machine code in real time, so you don't need source or need to compile with any particular options.

Just instrument your code with Purify (simplest way to do this: CC="purify cc" make), run your program, and get a nice gui that will show your leaks and other errors.

Available for Windows, Linux, and various flavors of Unix. There's a free trial download available.

http://www.ibm.com/software/awdtools/purify

like image 140
Mark Harrison Avatar answered Sep 20 '22 20:09

Mark Harrison