I have delegate
@property (nonatomic, assign) id <DelegateProtocol> delegate;
but it crash on performSelector
if (_delegate != nil && [_delegate conformsToProtocol:@protocol(DelegateProtocol)])
{
NSObject *obj = _delegate;
//HERE IS EXC_BAD_ACCESS
[obj performSelectorOnMainThread:@selector(didTouchImageView:) withObject:self waitUntilDone:NO];
}
I set delegate here:
- (void)viewDidLoad
{
[super viewDidLoad];
[invoiceTabImage setDelegate:self];
}
and the question is why it might be.
EXC_BAD_ACCESSmeans that your delegate was already deallocated when you sent it the messagedidTouchImageView(I assume that everything is ok when you send the messageperformSelector, it would be too easy).First of all, check the retain/release management for your delegate to see if there is anything incorrect.
If everything seems ok, one possibility to debug this is enabling Zombies (you can do through Instruments/Run with performance tool, or by setting an environment variable when debugging).
This could help you detect the cause of the problem.
If you need more help, please, post the code about how you create/retain/release your delegate object, and also paste the stack trace of the crash.
EDIT:
Two hints:
the key to working with delegates (without retaining them) is ensuring that the view controller (that in your case is also the delegate) lives longer than
invoiceTabImage; you can then reviewinvoiceTabImage‘s lifecycle (when it is created/released) and compare it to the delegate’s;in your controller’s
dealloc, add this line:invoiceTabImage = nil;so that you are ensuring that when the controller/delegate is removed, the delegating object knows that the delegate is not there anymore; the program will not work, but possibly will not crash.