Re: Event handle leak

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




> My multithread application leaks handle. But only when called from a while
> loop with no sleep.
> So the problem is, if I try to step in through the debugger theres no handle
> leak.
>
> The leaking handle according to ProcessExplorer is of type "Event".
>
> With ProcessExplorer I have the following information about the handle:
> 1. Handle Address
> 2. Access Address
> 3. Object Address
>
> So with this informations any way to break the code when the leaked event is
> created.

If you can test on Windows XP or 2003 (preferrably XP-SP2 or 2K3-SP1),
you can use handle tracing embedded into the OS to get call stacks at the moment
when a handle was created. This can be done with the help of WinDbg and related tools:
http://www.microsoft.com/whdc/devtools/debugging/default.mspx

If you can reproduce the problem when running the application under debugger
(WinDbg), run "!htrace -enable" command when you want to start tracing handles.
Then, after you see that handles are leaking, break into debugger
and use "!htrace <handle>" to display the call stack for the suspected handle
(use the real handle value in place of <handle>; 0 can be used to dump all handles).

If the problem cannot be reproduced under debugger, you can access the call stacks
noninvasively using these commands:

Enable tracing:
> cdb -pv -p <pid> -c "!htrace -enable;q"

Show call stack for a handle:
> cdb -pv -p <pid> -c "!htrace <handle>;q"

(<pid> means process id of the target process; access to good symbols for system DLLs
is needed, see Symbols section in WinDbg docs about symbol server etc.)

On earlier operating systems, you can try to trace calls to CreateEvent and record the call stack,
again with WinDbg, with a command like:
> bp kernel32!CreateEventW "kb;g"
(it will create a tracing breakpoint, which, when hit, will display the function parameters and
the call stack at the moment when the event was created and let the process continue, so you
can later use this information to find the source of the leaked event)

And of course another way is to use a product that can test your application
for resource leaks (e.g. see softwareverify.com).

Regards,
Oleg
[VC++ MVP]





.



Relevant Pages

  • RE: unhandled exception when opening window
    ... the reply by "Paul F. Williams" does seem to be relevant. ... > It seems that this is not the full call stack, and based on the call stack, ... > I suggest you use Windbg to run this application, ... > Does "Paul F. Williams"'reply in this message help you? ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Differences in unhandled exception handling between Windows 2000 and 2003?
    ... > What happens if you continue past the AV in windbg? ... I really don't think it's stack. ... but we establish our own stack limit that you all ... >> against that limit, and if the job exceeds it, that job gets an error and is reset. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Cant see call stack in WinDbg
    ... Those stack ranges certainly could be different threads, but without examining your process (or ... No idea - I'm really confused here because in WinDbg ... in WinDbg teh frame pointers go from 0012dbf4 to 0012ddc4. ...
    (microsoft.public.vc.debugger)
  • Re: Process handle leakage
    ... Take a look at the following link that explains how to use!htrace command ... in Windbg to detect handle leaks. ... Also you should be able to use AppVerifier to detect handle and memory leaks ... source code, i cannot find any function call like CreateProcess, and ...
    (microsoft.public.vc.mfc)
  • Re: Memory leak without handle leak?
    ... > I'm trying to debug a memory leak problem I'm having on windows 2003. ... > the stack trace for the leaks doesn't originate from my code. ...
    (microsoft.public.win32.programmer.kernel)