Using the PHD Class to Isolate Memory Leaks

Article translations Article translations
Article ID: 194655 - View products that this article applies to.
This article was previously published under Q194655
This article has been archived. It is offered "as is" and will no longer be updated.
Expand all | Collapse all

On This Page

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 files are available for download from the Microsoft Download Center:
Phd.exe
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. 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://docs.linux.cz/programming/c/www.eskimo.com/~scs/C-faq/q18.2.html
Note 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.

Properties

Article ID: 194655 - Last Review: February 21, 2014 - Revision: 2.5
APPLIES TO
  • 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
Keywords: 
kbnosurvey kbarchive kbdownload kbfile kbinfo kbsample KB194655

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com