Using the PHD Class to Isolate Memory Leaks

This article was previously published under Q194655
This article has been archived. It is offered "as is" and will no longer be updated.
Memory leaks are a problem in many applications. A concrete way ofdetermining memory leaks is to use the PHD Visual C++ class that isprovided in this article. By logging memory statistics in an applicationusing the PHD class, you can get a more precise idea of where a memory leakis occurring.

The self-extracting PHD.EXE is a sample that that demonstrates how to usethe included PHD class to prove/disprove a memory leak. The PHD class is athin wrapper around the Performance Helper Functions from the Windows NTresource kit. This code works only on Windows NT.
The following files are available for download from the MicrosoftDownload Center:
Release Date: Oct-23-1998

For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:
119591 How to Obtain Microsoft Support Files from Online Services
Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file.
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. Byincluding 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 fileto your path so that the application will find it.

The TestMain.cpp file contains the following sample code that demonstrateshow 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 youwant to monitor. These are the same items that are available in thePerfmon.exe application.

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

As you can see from the sample code, the PHD class is used to put thesuspected leaking API in a loop and periodically calls the PHD::logDatamethod. Data is logged to file with name "<your EXE name>_perf.log" in thecurrent directory. If the resulting log file shows a linear increase inprivate bytes, this increase does not imply a memory leak in the API, butmerely a memory leak in the application. If the API is used incorrectly(for example, by not correctly freeing resources allocated by the initialcall), the results show only a memory leak, not a faulty API. To narrowdown the problem, create the simplest possible application that exercisesthe 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, thefollowing FAQ mentions several products:Note The third-party products discussed here are manufactured by vendorsindependent of Microsoft; we make no warranty, implied or otherwise,regarding these products' performance or reliability. You should also notethat sometimes these products can make false reports of leaks, but theystill are very useful tools.

Also, the Visual C++ heap debugging API is a useful tool for locatingleaks. 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 functionsonly detect leaks in your code and not leaks in other components.

(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by RickAnderson, Microsoft Corporation.
leak hangs frozen

Article ID: 194655 - Last Review: 02/21/2014 00:30:31 - Revision: 2.5

  • Microsoft Visual Studio 6.0 Enterprise Edition
  • Microsoft Visual Studio 97 Service Pack 3
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • kbnosurvey kbarchive kbdownload kbfile kbinfo kbsample KB194655