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.
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.
The following files are available for download from the Microsoft Download Center:
Phd.exeRelease 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:
119591Microsoft 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.
(https://support.microsoft.com/kb/119591/EN-US/ )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:
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:
Other ToolsYou 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.htmlNote 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.
Article ID: 194655 - Last Review: February 21, 2014 - Revision: 2.5