Cleaning Up

To stop the application event loop, call the function QuitApplicationEventLoop(). The operating system calls QuitApplicationEventLoop() automatically if you tag the Quit menu item with the kHICommandQuit menu command ID. You can tag the Quit menu item in Interface Builder.

When the user quits your program, you may want to remove the event handlers you installed and the event handler universal procedure pointers (UPP) you created when you installed the event handlers. Call RemoveEventHandler() to remove an event handler. If you passed NULL as the event handler’s data reference when installing the event handler, you won’t be able to remove the event handler. Not being able to remove the event handler is why I used the term “may want to” instead of “should” at the start of the paragraph. Call DisposeEventHandlerUPP() to dispose of any event handler UPPs you created by calling NewEventHandlerUPP().

If you want your code to be perfect, store your event handler’s data reference and UPP variables. Remove your event handlers and dispose of your UPPs when the user quits your program. However, leaving your event handlers and UPPs alone is not a big problem. Technically, you have memory leaks if you don’t remove the event handlers and dispose of the UPPs. But the operating system disposes of all allocated memory when the user quits your application. Event handlers normally stay active for the entire time your program runs so the memory leaks aren’t a problem. If you were creating thousands of event handlers that stayed in effect for a short time, you would need to remove the handlers and dispose of the UPPs.

Conclusion

This article introduced you to Carbon events, but no single article can cover everything there is to know about them. For more information on Carbon events, read the Carbon Event Programming Guide and Carbon Event Reference documents. You can read these documents at Apple’s developer site or in Xcode by choosing Help > Documentation. These documents are in the Carbon documentation section under Events and Other Input.

Previous (Writing the Event Handler)