The CGEventTapCallback
documentation says
After the new event has been passed back to the event system, the new event will be released along with the original event.
Therefore you are not responsible for releasing the new event or the old event.
The analyzer doesn't know this, so you need to tell it. You can silence the analyzer by putting new
at the beginning of the method name, but maybe it would be clearer to use copy
, which also silences the warning:
- (CGEventRef)copyOrModifyKeyboardEvent:(CGEventRef)event {
(I guess newOrModifiedKeyboardEvent:
would work just as well.)
I believe the analyzer ought to flag this too, since it now fails to return a +1 reference count when you just return the input event. But in my testing, it doesn't. The Xcode 4.6.2 analyzer gives me no warnings for this method:
- (CGEventRef)copyOrModifyEvent:(CGEventRef)event {
CGEventRef newEvent;
if (CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode) == 38) {
newEvent = CGEventCreate(NULL);
} else {
newEvent = event;
}
return newEvent;
}