Memory Validator provides several complementary methods for detecting memory leaks and handle leaks. The usual method is the end-to-end analysis of an application, watching all allocations and deallocations. Any allocations that are not deallocated by the end of the application’s lifetime are deemed to have leaked.
An alternative to this is detecting memory leaks and handle leaks as an application executes. This is called in-place leak detection. When using in-place leak detection, Memory Validator provides you with a selection of options so that you can tailor the leak detection to your needs.
This tutorial will use the nativeExample.exe sample application to demonstrate memory leak detection. The nativeExample.exe application is shipped as a project with source code. The project can be found in the nativeExample directory in the same directory that Memory Validator was installed in. Please compile all configurations of the sample application before continuing the tutorial. The project file is nativeExample.dsp.
To use the in-place leak detection facilities, your application must be running.
Select [installdir]\nativeExample\debugNonLink\nativeExample.exe using the Browse… button. Accept all default settings and move through all pages of the wizard to the last page. Click the Start Application button.
CString **csa; CString *str; csa = new CString *[2]; str = new CString(); csa[0] = str csa[1] = NULL;
In this example csa and str is leaked, but the CString object str is pointed to by the pointer in csa[0], Memory Validator will find this reference and thus csa will be marked potentially leaked whilst str will not be marked as potentially leaked.
To use the in-place leak detection facilities, your application must be running.
The insert watermark dialog is displayed. Type a meaningful name into the edit box to describe the watermark. For this demo, type beforeDemoLeak.
Display the About box for the application, by choosing About nativeExample… on the Help menu. The example application leaks some memory when the About box is displayed.
Insert another watermark after the most recent allocation by choosing Add watermark at most recent trace on the Managers menu. The insert watermark dialog is displayed. Type a meaningful name into the edit box to describe the watermark. For this demo, type afterDemoLeak.
We now have some allocations with watermarks before and after the allocations.
To check for memory leaks and handle leaks in the application, click Detect Leaks.
Memory Validator examines the list of allocations it knows about and prepares a list of allocations to check for all allocations found between the specified watermarks. A dialog is displayed informing you of the number of allocations that will be checked.
After the dialog is dismissed, the display on the Memory tab is updated to indicate which allocations have leaked. Note, that you must have the appropriate data display option enabled (i.e. CRT for C++ allocations, LocalAlloc for allocations with LocalAlloc(), etc).