I'm getting BAD_ACCESS
when attempting to instantiate an NSURLConnection
in a class that's called from my view controller.
I've implemented the delegate methods in the class, and the interface is a subclass of NSObject <NSURLConnectionDelegate>
.
I can't for the life of me figure out what the problem is:
NSURL *url = [NSURL URLWithString:@"http://valid-value-here"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init ];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request addValue:@"application/json;chartset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSString *myJSONString = @"valid-json-string";
NSData *myJSONData = [myJSONString dataUsingEncoding:NSUTF8StringEncoding];
NSUInteger cCount = [myJSONString length];
[request addValue:[NSString stringWithFormat:@"%d", cCount] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:myJSONData];
NSURLResponse *theResponse = [[NSURLResponse alloc]init];
NSError *theError = nil;
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
When the code hits the last line above, the exception occurs.
I'm a .Net developer most of the time, so my familiarity with XCode is sub-par, and I'm also relatively new to Objective-C, so any help is appreciated!
EDIT
Here is the output from the ZombieEnabled: * -[AccountApi Login:password:onSuccess:onFail:]: message sent to deallocated instance 0x71427c0
That is the method that this NSURLConnection
lives inside. It is called via:
loginApi = [[AccountApi alloc] init];
[loginApi Login: @"" password:@"" onSuccess:^{} onFail:^{}];
With loginApi
declared as AccountApi *loginApi;
above the @implementation
for the view controller.
Edit 2
I've declared my reference to AccountApi
as an instance variable via:
@implementation LoginViewController {
AccountApi *lApi;
}
Then in the function:
- (void)callApi
{
lApi = [[AccountApi alloc] init];
NSString *login = userLogin.text;
NSString *pass = userPassword.text;
[lApi Login: login password: pass onSuccess:^{
[self successBlock]; // For example...
} onFail:^{
[self failBlock]; // For example...
}];
}
This instantly still produces: * -[AccountApi Login:password:onSuccess:onFail:]: message sent to deallocated instance 0x7154ba0
Edit 3
I've confirmed that lApi
is already a zombie by the time it gets to the Login
call. How is it deallocating in 3 lines of code from: lApi = [[AccountApi alloc] init];
to the call?
Solved
In AccountApi
my init was missing something....ugh:
Was:
- (id)init {
}
Should be:
- (id)init {
return self;
}