内存泄漏错误

发布于 2024-12-09 04:33:04 字数 1998 浏览 0 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2024-12-16 04:33:04

如果第 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文