Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclassing UIApplication to override sendEvent causes crash

I'm trying subclass UIApplication to catch all touch event, this is used to see if user is afk or not. Anyway it works great first time you launch the application. If you put it in background and open it again 2 times then it crashes. I have no idea what causes this. I'm getting EXEC_BAD_ACCESS on [super sendEvent:event];

My Subclass MyUI:

@implementation MyUI

- (void)sendEvent:(UIEvent *)event {
    [super sendEvent:event]; // <-- EXEC_BAD_ACCESS

    if (event.type == UIEventTypeTouches) {
        UITouch *touch = [event allTouches].anyObject;
        if (touch.phase == UITouchPhaseBegan) {
           // Calling some methods
        }
     }
}  
@end

Main.m

int main(int argc, char *argv[])
{
    NSString* appClass = @"MyUI";
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, appClass, nil);
    [pool release];
    return retVal;
}
like image 298
David Avatar asked May 02 '11 01:05

David


2 Answers

Is there a reason the super class method is being called first? If you are intercepting, you should be calling it last.

That might be your problem. I also override sendEvent to intercept all events to my application and have no issues.

If you call the super method first it will pass it to all UIResponders that may eventually eat your event resulting in the EXEC_BAD_ACCESS. Furthermore as DavidNeiss suggests eliminate the lines below the super call. If you still receive the bad access signal its likely another view or controller down the line causing it. You'll need to stack trace to find out where it is.

like image 192
James C Avatar answered Sep 19 '22 09:09

James C


To get exact reason for EXEC_BAD_ACCESS use nszombieenabled in your application. This link will guide you for using it. http://iosdevelopertips.com/debugging/tracking-down-exc_bad_access-errors-with-nszombieenabled.html

like image 24
Rony Avatar answered Sep 19 '22 09:09

Rony