Memory Leak Detection in C++

Don't put off fixing memory leaks. Make one or more of these convenient tools a part of your development process.
Insure++

The Insure++ product by Parasoft is not GPLed or free software, but it is a good tool for memory leak detection and code coverage, very similar to mpatrol. Insure++ does do more than mpatrol in the area of code coverage and provides tools that collect and display data. Trial copies of the software can be downloaded and tried for a specified time period on non-Linux workstations.

The product installs easily under Linux but is node-locked to the computer on which it is installed. Insure++ comes with a comprehensive set of documentation and several options. The code coverage tool is separate but comes with the initial package.

Insure++ provides a lot of information about the problems it finds. To use Insure++, it is necessary to compile it with the Insure++ front end, which passes it to the normal compiler. This front end instruments the code to use the Insure++ library routines. During the compiler phase, illegal typecasts are detected as well as incorrect parameter passing. Obvious memory corruption errors are reported. During runtime, errors are reported to stderr but can be displayed by a graphical tool. When building an application, either the command line or makefiles can be used, facilitating the building of projects and large applications.

Execution of the program is simple. Insure++ does not require any special commands to execute; the program is run as if it were a normal program. All the debug and error-trapping code is contained in the Insure++ libraries that were linked with the program.

An add-on tool, called Inuse, displays in real time how the program uses memory. It can give an accurate picture of how memory is used, how fragmented it gets and subtle leaks that seem small but could add up over time. I had an experience with a client who found that a particular C++ class was leaking a small amount of memory that, on a workstation, was seen to be quite small. For an embedded system that was expected to be running for months and possibly years, the leak could become quite large. With this tool, the leak was easily traced, found and fixed. Other available tools did not catch this leak.

Code coverage is analyzed by another tool, TCA. As the program is run with Insure++ turned on, data can be collected that, when analyzed by TCA, paints an accurate picture of what code was executed. TCA has a GUI that enhances the display of code coverage.

Resources

Cal Erickson (cal_erickson@mvista.com) currently works for MontaVista Software as a senior Linux consultant. Prior to joining MontaVista, he was a senior support engineer at Mentor Graphics Embedded Software Division. Cal has been in the computing industry for over 30 years, with experience at computer manufacturers and end-user development environments.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

The easiest way to prevent

Rammstein's picture

The easiest way to prevent memory leaks is to use ready utils, like Deleaker ( http://deleaker.com/ )

Detecting Memory leak in Visual Studio (Debug mode)

Anonymous's picture

The memory leak information is printed on "Output" window.
[ This advantage is in "Debug" mode only. ]
Example

int _tmain()
{
int* ptr = NULL;

ptr = (int*)malloc(sizeof(int)*20);

for( int i=0; i<20; ++i )
ptr[i] = i;

#ifndef NDEBUG

int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); // Get current flag

flag |= _CRTDBG_LEAK_CHECK_DF; // Turn on leak-checking bit

_CrtSetDbgFlag(flag); // Set flag to the new value

#endif

//free(ptr);

printf("\n\nPress Enter...");
getch();
return 0;
}

http://www.mindfiresolutions.com/Detecting-Memory-leak-in-Visual-Studio-...

INSOMNIA :Coding contest

Anonymous's picture

IIT ROORKEE presents INSOMNIA:THE MIDNIGHT PROGRAMMING CONTEST...
www.insomnia.cognizance.org.in
Starts on : 27th March, 9:00 PM

Cash prizes worth Rs.30,000 on stake for 3rd round.

is there are any tools to prevent the memory leaks

newbie's picture

I want to know is any tool for detect the memory leaks which for windows platform and it is able to include in the some other tools (we program are excuted...)

The best one is

Bill Gates's picture

The best one is Deleaker!
Deleaker is a run-time error detection and debugging tool for Visual C++ developers. Deleaker is a really useful add-in for Visual Studio that helps you to analyze programming errors, many of which are unique to Visual C++. With Deleaker you can detect and localize resource leaks such as memory, GDI and USER objects, handles.
http://deleaker.com

all thos tools sucks

Anonymous's picture

the best way to discover memory leaks:
hard core debugging

one more tool for debugging memory leaks-Coverity Prevent

victor louis's picture

You can prevent memory leaks by watching for some common problems. Collection classes, such as hash tables and vectors, are common places to find the cause of a memory leak. This is particularly true if the class has been declared static and exists for the life of the application. . The prevalence of memory leak bugs has led to the development of a number of debugging tools to detect unreachable memory. Coverity Prevent is one of the tool that you can use for fixing these kinds of bugs. Coverity Prevent is also used by the Department of Homeland security to scan many open source projects. You can get more info at http://www.Coverity.com

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState