Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a handle leak detector which can be linked into an existing application?

I'm involved in various C++ projects (mostly using MSVC6 up to MSVC10) in which we recently found a few handle leaks (thread handles as given by the CreateThread function). I suspect that there are plenty of other handles being leaked as well and I'd like to integrate a test which verifies that no handles are leaked into our nightly test results.

My idea was to develop a DLL which instruments the relevant kernel32.dll functions (CreateThread, OpenProcess, CreateProcess and a dozen more) as well as the CloseHandle function. The DLL would then, for each handle being acquired, memorize a backtrace. At the end of the process, the DLL would print all backtraces of handles which weren't closed to some sort of log file, which could then be parsed by the test framework.

This will of course also yield backtraces for all handles which are still reachable (so technically, they didn't leak - maybe the author intended that the OS reclaims them when the process terminates) but I Guess explicitely closing them doesn't hurt - especially since we already have some nice RAII wrappers for this stuff, we just don't use it as much as we should.

Now I'm wondering - this seems like a fairly straightforward approach; maybe somebody here is alware of a library which already does this?

like image 798
Frerich Raabe Avatar asked Nov 22 '11 08:11

Frerich Raabe


People also ask

What is a leak detector?

Types Of, Uses This article takes an in depth look at leak detectors. What is a leak detector? And much more… Chapter One – What is a Leak Detector? A leak detector is a sensor used for monitoring a system that contains liquids, gasses, or other substances to ensure that the contained materials do not escape, leak, or otherwise leave the system.

What is a helium leak detector?

A helium leak detector, also known as a mass spectrometer leak detector (MSLD), locates and measures leaks using helium as a tracer gas. The helium gas is introduced into the system under pressure, which is measured to determine if there are leaks. The use of helium leak detectors is related to vacuum and pressure testing.

How does the Honeywell water leak detection system work?

Not only does it detect water leaks but it will also alert you to freezing temps and humidity issues. The Honeywell integrates with Google Home, Amazon Alexa, Apple HomeKit, and IFTTT. The cable sensor detects water along its entire length (4 feet to start, but you can extend that with additional cables).

What kind of gas leak detector do I Need?

A versatile gas leak detector will assist you in finding leaks from multiple combustible gases, including methane, natural gas, propane, and more. The CD100A Combustible Gas Leak Detector from UEi Test Instruments is a top choice for homeowners looking for a handheld and easily operated device. The UEi CD100A uses a semiconductor sensor ...


2 Answers

This is definitely possible, but I don't think there's a library that does it yet.

The easiest way, I think, would be with Application Verifier. You can get it from Microsoft's Debugging Tools for Windows. Configure it to track your application's handles, run your application in a debugger for a bit, and then a list of handles will be dumped when your application exits.

Another way to do it, without Application Debugger, would be to set a breakpoint or pause before your application exits. While the application is paused, use something like Process Explorer to obtain a list of all open handles.

For your purposes, I think the latter would be the better choice. I'm not sure of any automated tools that use the debug output. You can use some functionality of the WDK to retrieve a list of the current process (or another process's) open handles, but it is a bit complicated.

like image 104
Collin Dauphinee Avatar answered Oct 23 '22 22:10

Collin Dauphinee


Mark Russinovich explains in his series to Pushing the Limits of Windows how to better deal with handles and how to track handle leaks.

He is mentioning Windows Debugger and Application Verifier, and he is explaining how you can use that to track your handle leaks down.

In the same page, he mentions also a neat feature of his well-known Process Explorer that is flashing green and red for processes creating / closing handles.

like image 25
Zeugma Avatar answered Oct 23 '22 22:10

Zeugma