I am very new new to iPhone app development and not able to correct this error when I ran leak analyzer (XCode->Product->Analyze) over my code. And it showed me potential leak of an object on some line.
1) Method returns an objective c object with a +1 retain count (owing reference)
2) object allocated on line 128 is not referenced later in this execution path and has retain count of +1(object leaked).responeData is retained in Property declaration part
-(IBAction)registerButtonPressed:(id)sender
{
self.responseData = [NSMutableData data];
NSString *username = txtUsername.text;
NSString *jsonstring = [NSString stringWithFormat:@"http://demo.elgghub.com/apis/services/api/rest/json/?method=register&username=%@",username];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:jsonstring]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[connection release];
self.responseData = nil;
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error"
message:@"Please check your network connection and relaunch the application"
delegate:self
cancelButtonTitle:@"Dismiss"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseStringReg = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
self.responseData = nil;
NSDictionary *login =(NSDictionary*)[responseStringReg JSONValue] ;
[responseStringReg release];
NSNumber *status = [login objectForKey:@"status"];
NSString *statusString = [status stringValue];
NSString *message = [login objectForKey:@"message"];
}
-(void)dealloc
{
[responseData release];
[demoView release];
[super dealloc];
}
If line 128 is where you create the NSURLConnection the warning appears because the analyzer has no way of knowing that it will be released on the delegate. You should probably be storing a reference to it anyways, perhaps in an instance variable.