An easy way to find descriptor leaks is to record everything.
Each time you receive a descriptor, register it as you received it, as well as any other information about the circumstances. Each time you release a handle, register that you released it. Include both times the actual descriptor (only one hex).
Then you will get a log that looks like this (for example):
Obtained handle 0xf000 (nLineNo = 5) Obtained handle 0xb000 (nLineNo = 6) Obtained handle 0xd0d0 (nLineNo = 7) Released handle 0xf000 Released handle 0xb000
If you select this manually, you will see that you received the 0xd0d0 handle when nLineNo was 7 and it was never released. It is not so much, but it helps, and if the situation becomes tough, you can even try to record stack traces in each receive / release. In addition, if the log is always reliably created in this way, you can start entering breakpoints based on the actual values ββ(for example, a break at a point in the program when the handle is 0xd0d0 so you can see what happens to it).
If this is more practical, you can start wrapping your pens inside the program itself, for example. a std::set all the pens received, as well as any details about when they were received, and you can effectively hack your program to keep track of what it is doing (then undo all your changes after fixing).
Hope this helps - this is part of the reason why I at least keep the std::set everything I get, so if the worst comes to the worst, you can iterate over them when turned off and release them all (and write big "FIX THIS!"!)
source share