内存泄漏错误
我对 iPhone 应用程序开发非常陌生,当我在代码上运行泄漏分析器(XCode->Product->Analyze)时,无法纠正此错误。它向我展示了某些线路上可能存在物体泄漏的情况。
1) 方法返回一个目标 c 对象,其保留计数为 +1(欠引用)
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];
}
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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果第 128 行是您创建 NSURLConnection 的位置,则会出现警告,因为分析器无法知道它将在委托上释放。无论如何,您可能应该存储对它的引用,也许在实例变量中。
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.