SAMPLE: Using the PHD Class to Isolate Memory Leaks

ID: Q194655


The information in this article applies to:


SUMMARY

Memory leaks are a problem in many applications. A concrete way of determining memory leaks is to use the PHD Visual C++ class that is provided in this article. By logging memory statistics in an application using the PHD class, you can get a more precise idea of where a memory leak is occurring.

The self-extracting PHD.EXE is a sample that that demonstrates how to use the included PHD class to prove/disprove a memory leak. The PHD class is a thin wrapper around the Performance Helper Functions from the Windows NT resource kit. This code works only on Windows NT.


MORE INFORMATION

The following file is available for download from the Microsoft Software Library:

~ PHD.exe
Release Date: Oct-23-1998

For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:
Q119591 How to Obtain Microsoft Support Files from Online Services

The following key files are included in the sample:

    FileName                Description
    ---------------------------------------------------------
   PDH.dll                  Contains helper functions used by PHD.
   RKLeak.cpp               Contains the code for the PHD class.
   RKLeak.h                 Header file for the PHD class.
   TestMain.cpp             A simple program to demonstrate the PHD class. 

To use the PHD class, include the RKLeak.cpp file in your project. By including the header file, you will cause linkage to the PDH.lib library, which comes with the Visual C++ 6.0. You also need to add the PHD.dll file to your path so that the application will find it.

The TestMain.cpp file contains the following sample code that demonstrates how to use the PHD class:

Sample Code


   **************************************************
   #include "rkLeak.h"

   void main()
   {

      char *myCntrs[]={
         "\\Memory\\Available bytes",
         "\\Memory\\committed bytes",
         "\\Memory\\pool Paged bytes",
         "\\Memory\\pool Nonpaged bytes",
      };

      PHD mphd2(myCntrs,sizeof(myCntrs)/sizeof(myCntrs[0]));
      PHD mphd;

      const int arSize=4096;
      for (int i=0;i<5;i++){
         double *d = new double[arSize];
         d[0]= 1.;      d[arSize-1] = 2.;  // Force to committed mem.
         mphd.logData(i);
         mphd2.logData(i);
      }

   *************************************************** 

The first use of the PHD constructor allows you to specify which items you want to monitor. These are the same items that are available in the Perfmon.exe application.

The second use of the PHD constructor (no arguments) uses the following default column logs: private bytes, page file bytes, pool paged bytes, pool nonpaged bytes, and working set.

As you can see from the sample code, the PHD class is used to put the suspected leaking API in a loop and periodically calls the PHD::logData method. Data is logged to file with name "<your EXE name>_perf.log" in the current directory. If the resulting log file shows a linear increase in private bytes, this increase does not imply a memory leak in the API, but merely a memory leak in the application. If the API is used incorrectly (for example, by not correctly freeing resources allocated by the initial call), the results show only a memory leak, not a faulty API. To narrow down the problem, create the simplest possible application that exercises the API in question.

Visual C++ 5.x users need to comment out the following lines from RKLeak.h:

   #undef PdhOpenQuery      //          PdhOpenQueryA
   extern "C" long __stdcall
   PdhOpenQuery (
       IN      LPCSTR      szDataSource,
       IN      DWORD       dwUserData,
       IN      HQUERY      *phQuery
   ); 

Other Tools

You can search on the Web in FAQ pages for information on various third- party debuggers that can help you find memory leaks. For example, the following FAQ mentions several products:
http://phantom.iweb.net/docs/C/faq/q18.2.html
The third-party products discussed here are manufactured by vendors independent of Microsoft; we make no warranty, implied or otherwise, regarding these products' performance or reliability. You should also note that sometimes these products can make false reports of leaks, but they still are very useful tools.

Also, the Visual C++ heap debugging API is a useful tool for locating leaks. These include functions, such as _CrtMemDifference() and _CrtMemDumpAllObjectsSince(), which are documented in the Visual C++ Programmer's Guide in the Debug Function Reference section. These functions only detect leaks in your code and not leaks in other components.

(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Rick Anderson, Microsoft Corporation.

Additional query words: leak hangs frozen


Keywords          : kbfile kbsample kbVS97 kbVS600 
Version           : WINDOWS:6.0,97
Platform          : WINDOWS 
Issue type        : 

Last Reviewed: April 13, 1999