内存泄漏错误

时间:2011-10-11 05:52:28

标签: iphone memory memory-leaks

我是iPhone应用程序开发的新手,当我在代码上运行泄漏分析器(XCode-> Product-> Analyze)时无法纠正此错误。它显示了某个线上某个物体的潜在泄漏。

1)方法返回一个具有+1保留计数的目标c对象(欠参考)

2)第128行分配的对象在此执行路径中未被引用,并且保留计数为+1(对象泄漏).responeData保留在属性声明部分

-(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];
}

1 个答案:

答案 0 :(得分:0)

如果第128行是您创建NSURLConnection的位置,则会出现警告,因为分析器无法知道它将在代理上释放。您可能应该存储对它的引用,可能在实例变量中。