Is there a pen leak sensor that can be linked to an existing application?

I participate in various C ++ projects (mainly using MSVC6 to MSVC10), in which we recently discovered several handle leaks (thread handles set by CreateThread ). I suspect there are many other pens that also leak out, and I would like to integrate a test that checks that no pens fall into our nightly test results.

My idea was to develop a DLL that equips the corresponding kernel32.dll functions (CreateThread, OpenProcess, CreateProcess and a dozen more), as well as CloseHandle . Then, the DLL for each descriptor being processed remembers the return line. At the end of the process, the DLL printed all the traces of the descriptors that were not closed to any log file, which can then be analyzed using a test environment.

This, of course, will also give backtraces for all descriptors that are still available (so they didn’t leak technically - perhaps the author assumed that the OS restores them when the process ends), but I think that closing them is clearly not because that we already have some good RAII shells for this material, we just don't use it as much as we should.

Now I wonder - this seems like a pretty simple approach; maybe someone here maybe identifies a library that is already doing this?

+6
source share
5 answers

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

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

Another way to do this without a debugger application is to set a breakpoint or pause before your application exits. While the application is paused, use something like Process Explorer to get a list of all open descriptors.

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

+3
source

Mark Russinovich explains in his series “Pushing Windows Limits” how to deal better with processes and how to track descriptor leaks.

He mentions Windows Debugger and Application Verifier, and he explains how you can use this to track handle leakage.

On the same page, he also mentions a neat feature of his famous Process Explorer, which flashes green and red for descriptor creation / closing processes.

+2
source

If you read the article “Clicking Constraints for Windows”, you would see that it mentions the WinDbg extension! htrace, which, it seems to me, fulfills your first requirement for the toolkit of the corresponding kernel32.dll functions related to creating the descriptor.

To automate the call on! htrace, you can embed the debugger engine in the test harness or you can use something like PyDbgEng to launch your application and cause the extension! htrace, and then collect the stacks when the application ends. There is an example of this type of automation with PyDbgEng, but with registry functions at http://pydbgeng.svn.sourceforge.net/viewvc/pydbgeng/trunk/PyDbgEng/Examples/RegMon.py?view=markup , but I think you could To use this example to call an extension, see (dbg.idebug_control.CallExtension) in this example.

+1
source

We use Memory Validator at my workplace. It can be configured to track each memory allocation, COM call (and number of links), and pens. You run it and run the code necessary to verify or attach to the current code that you want to verify. From now on, he keeps track of all the resources that you told him. When the code exits, it will have a report of any resources that it tracked that were not allocated, and where they were allocated. This is a commercial product, but it has a trial period, so you can check it and see if it meets your needs. It was somewhat useful for me and my colleagues when we had problems with leaks in difficult situations, but this is not a magical solution for ourselves. Good leak detection methods still need to be followed, it just gives you more information about which distributions still exist and where they came from.

+1
source

I played with Deleaker and was successful with memory and leak handling. It can be used in Visual Studio or on its own. On the Deleaker website:

It does not matter what type of leaks occurred, Deleaker finds them all: memory leaks (created by heap, virtual memory or OLE allocators, etc.), GDI leaks, USER USER objects and descriptor leaks. "

0
source

Source: https://habr.com/ru/post/902083/


All Articles