HTTP 请求内存泄漏

发布于 2024-11-27 10:52:33 字数 1362 浏览 0 评论 0原文

我正在使用这段代码,但在分析时,它告诉我在 response_errorrequest_response 变量中存在许多内存泄漏。

我尝试了几个地方来放置函数中使用的每个变量的释放代码,但它也不断崩溃,有或没有错误消息。 (最常见的是 EXC_BAD_ACCESS 指向内存访问错误)

我认为这可能是 NSURLConnection sendSynchronousRequest 方法的问题,但我不确定。

有人可以给我建议或将 release 块放置在这段代码的正确位置吗?

谢谢,

NSString *request_url = [NSString stringWithFormat:@"http://www.server.com/api/arg1/%@/arg2/%@/arg3/%@",self._api_key,self._device_id,self._token];
NSURL *requestURL = [NSURL URLWithString:request_url];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:requestURL];
NSError *response_error = [[NSError alloc] init];
NSHTTPURLResponse *_response = [[NSHTTPURLResponse alloc] init];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&_response error:&response_error];
NSString *str_response = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
return [[str_response JSONValue] valueForKey:@"pairing"];

变量的定义如下

@interface MyClass : NSObject {
  NSString *_device_id;
  NSString *_token;
  NSString *_api_key;
}
@property (nonatomic,retain) NSString *_device_id;
@property (nonatomic,retain) NSString *_api_key;
@property (nonatomic,retain) NSString *_token;

I'm using this code, but when profiling, it tells me I have a many memory leaks within response_error, request and _response variables.

I tried several places to put a release code of each variable used in function, but it keeps crashing with and without error message too. (most often it is EXC_BAD_ACCESS which points to memory access error)

I think it could be problem of NSURLConnection sendSynchronousRequest method, but I'm not sure.

Can somebody please give me an advice or place release blocks in right place of this code?

Thanks

NSString *request_url = [NSString stringWithFormat:@"http://www.server.com/api/arg1/%@/arg2/%@/arg3/%@",self._api_key,self._device_id,self._token];
NSURL *requestURL = [NSURL URLWithString:request_url];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:requestURL];
NSError *response_error = [[NSError alloc] init];
NSHTTPURLResponse *_response = [[NSHTTPURLResponse alloc] init];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&_response error:&response_error];
NSString *str_response = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
return [[str_response JSONValue] valueForKey:@"pairing"];

where variables are defined like

@interface MyClass : NSObject {
  NSString *_device_id;
  NSString *_token;
  NSString *_api_key;
}
@property (nonatomic,retain) NSString *_device_id;
@property (nonatomic,retain) NSString *_api_key;
@property (nonatomic,retain) NSString *_token;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

冷情 2024-12-04 10:52:33

您通过不必要的分配来泄漏 _responeresponse_error 。您将一个指向您的指针的指针传递给一个方法,该方法只会更改该指针,从而产生泄漏。您还需要自动释放 str_response

NSError *response_error = nil; //Do not alloc/init
NSHTTPURLResponse *_response = nil; //Do not alloc/init
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&_response error:&response_error];
NSString *str_response = [[[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding] autorelease];
return [[str_response JSONValue] valueForKey:@"pairing"];

You are leaking _respone and response_error by needlessly allocating them. You are passing a pointer to your pointer to a method that will just change the pointer creating a leak. Also you need to autorelease str_response

NSError *response_error = nil; //Do not alloc/init
NSHTTPURLResponse *_response = nil; //Do not alloc/init
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&_response error:&response_error];
NSString *str_response = [[[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding] autorelease];
return [[str_response JSONValue] valueForKey:@"pairing"];
死开点丶别碍眼 2024-12-04 10:52:33

如果您调用alloc/init,然后不调用release 或autorelease,则很可能会泄漏内存。

If you are calling alloc/init and then not calling release or autorelease, odds are you are going to leak memory.

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