Reputation: 3784
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.
Upvotes: 3
Views: 7963
Reputation: 71
Just check if it is being set to nil (which is what dealloc will do).
This solved the problem for me:
- (void)setDelegate:(id<UITableViewDelegate>)newDelegate
{
if (newDelegate!=nil) {
if (newDelegate != self.collapseDelegate)
{
self.collapseDelegate = newDelegate;
[super setDelegate:self.collapseDelegate?self:nil];
}
}
}
Upvotes: 1
Reputation: 69027
EXC_BAD_ACCESS
means that your delegate was already deallocated when you sent it the message didTouchImageView
(I assume that everything is ok when you send the message performSelector
, 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 review invoiceTabImage
'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.
Upvotes: 5