HTTP 请求内存泄漏
我正在使用这段代码,但在分析时,它告诉我在 response_error
、request
和 _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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您通过不必要的分配来泄漏
_respone
和response_error
。您将一个指向您的指针的指针传递给一个方法,该方法只会更改该指针,从而产生泄漏。您还需要自动释放str_response
You are leaking
_respone
andresponse_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 autoreleasestr_response
如果您调用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.