Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an Event Handle?

I have a handle leak in a big old program. Using sysinternals handle.exe I deduced that the type of handle that is leaking is an "Event" handle. But I'm not sure what parts of my code I should be looking at. Is there a list somewhere of functions that return handles to events?

EDIT: There is not a single instance of CreateEvent, CreateEventEx or OpenEvent in the entire program.

like image 460
Mick Avatar asked Nov 17 '09 17:11

Mick


People also ask

What is meant by event handling?

What is Event Handling? Event Handling is the mechanism that controls the event and decides what should happen if an event occurs. This mechanism have the code which is known as event handler that is executed when an event occurs. Java Uses the Delegation Event Model to handle the events.

What is event handling explain with example?

Events are generated as result of user interaction with the graphical user interface components. For example, clicking on a button, moving the mouse, entering a character through keyboard,selecting an item from list, scrolling the page are the activities that causes an event to happen.

What is event handle in C#?

An event handler, in C#, is a method that contains the code that gets executed in response to a specific event that occurs in an application. Event handlers are used in graphical user interface (GUI) applications to handle events such as button clicks and menu selections, raised by controls in the user interface.

What are the functions of event handling?

Event handlers can be used to handle and verify user input, user actions, and browser actions: Things that should be done every time a page loads. Things that should be done when the page is closed. Action that should be performed when a user clicks a button.


2 Answers

How much of these leaked handles do you see?

Events are created implicitly by critical sections (see InitializeCriticalSection et.al.) and, probably, some other Win32 elements which I can't remember at the moment. Also, they can be created by the framework you are using (if any) such as MFC, or by libraries you are using.

To track down the leak, you can use a print-only breakpoint. Step into CreateEvent function (using assembly view) and put a breakpoint on its first instruction. Then right-click the breakpoint, choose 'When Hit...' and edit the options so it won't break into debugger but will print some useful information (e.g. see $CALLER macro). Then run your app... and be prepared to see a HUGE log. If there is a true leak, you'll see a repeating pattern in the log, which identifies the offender.

like image 196
atzz Avatar answered Oct 09 '22 13:10

atzz


As other people have mentioned, CreateEvent / CreateEventEx are used to create "Event" handles. These represent synchronization objects that are frequently used to gate access, provide signals (potentially) to other threads, and may also be used as the basis for locks.

If you're trying to debug a leak involving Event handles, you should try to look for places where CreateEvent(Ex) is called without a corresponding CloseHandle(). Depending on what frameworks you used to obtain Events, you may also find that you might just be missing them on cleanup if they are members of another object/structure (Ex. something that has a generic HANDLE member variable that is skipped on cleanup, or a pointer to a HANDLE, etc).

If you don't recall having created these objects in your own code, it is possible you are missing a analogous Close() or other cleanup method on another class or provider that uses them internally. Things that do background processing, signaling, or provide methods to wait for operations to finish are the usual suspects here.

Create Event Handles
CreateEvent Function @ MSDN
CreateEventEx Function @ MSDN

Cleanup Handles
CloseHandle Function @ MSDN

like image 25
meklarian Avatar answered Oct 09 '22 13:10

meklarian