Reputation: 3670
I've got the following (relevant portions of an) object:
#import "HTTPHelper.h"
#define TIME_OUT_SECONDS 30
@interface HTTPHelper (Private)
- (ASIHTTPRequest *)sendRequest:(ASIHTTPRequest *)request error:(NSMutableArray*)error;
@end
@implementation HTTPHelper
@synthesize delegate;
- (void) invokeSessionTimeout {
[ASIHTTPRequest clearSession];
if ( delegate != nil ) {
[delegate sessionTimedOut];
}
}
- (ASIHTTPRequest *)sendRequest:(ASIHTTPRequest *)request error:(NSMutableArray*)error {
if ( request == nil ) {
return nil;
}
[request setTimeOutSeconds:TIME_OUT_SECONDS];
[request setValidatesSecureCertificate:NO];
[request startSynchronous];
if ( [request responseStatusCode] == 0 ) {
return nil;
}
if ([request responseStatusCode] < 200 || [request responseStatusCode] >= 400) {
if ( [request responseStatusCode] == 403 && [error count] == 0 && delegate != nil ) {
NSLog(@"session has timed out.");
// CRASH HAPPENS HERE:
[self performSelectorOnMainThread:@selector(invokeSessionTimeout) withObject:nil waitUntilDone:YES];
return nil;
}
}
return request;
}
@end
Here's the header:
#import "ASIHTTPRequest.h"
@protocol HTTPHelperDelegate
- (void) sessionTimedOut;
@end
@interface HTTPHelper : NSObject {
id<HTTPHelperDelegate> delegate;
}
@property (nonatomic, assign) id<HTTPHelperDelegate> delegate;
@end
It crashes on the line with the performSelectorOnMainThread
call. I thought that was the problem at first, because this is happening on the main thread, but the documentation says you should use this method if you don't know what thread is going to be calling your method, so that shouldn't be it.
I have mostly been trying to make sure delegate is a pointer to my object. It's obvious not nil, (and it's not false, either). When I crash and hover over the delegate, it says "struct objc_object * delegate 0x65696d0"... but when I print that object in the debugger, it says "0x65696d0 does not appear to point to a valid object."
I am setting the delegate to nil in the viewDidUnload
methods of the two view controllers that set themselves as the delegate. Should I be using retain
instead of assign
?
Am I doing something else obviously wrong here?
Thanks for reading.
Upvotes: 0
Views: 883
Reputation: 63707
EXC_BAD_ACCESS usually happens when you try to access an over released object. In this case it is the delegate
ivar. Either you didn't set any value, or the object wasn't retained. Note that a released object is not nil unless you set it to nil, so even if it has a value it will be a random memory value, and you shouldn't touch it other than to set a value.
Upvotes: 1