ASIHTTPRequest内存泄漏 [英] ASIHTTPRequest memory leaks
问题描述
我有一个iOS项目,在我自己的班级中使用ARC,但是在其他库(例如ASIHTTPRequest
)中关闭了ARC.
I have a iOS project in which I am using ARC in my own classes, but have ARC turned off in other libraries like ASIHTTPRequest
.
使用下面的代码从Web服务器获取图像时,我正在发生大量内存泄漏:
I'm getting huge memory leaks using the code below to fetch an image from a web server:
-(void)buildPhotoView {
self.photoLibView.hidden = NO;
NSString *assetPathStr = [self.cellData objectForKey:@"AssetThumbPath"];
// get the thumbnail image of the ocPHOTOALBUM from the server and populate the UIImageViews
NSURL *imageURL = [NSURL URLWithString:assetPathStr];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:imageURL];
__unsafe_unretained ASIHTTPRequest *weakRequest = request;
[weakRequest setCompletionBlock:^{
// put image into imageView when request complete
NSData *responseData = [weakRequest responseData];
UIImage *photoAlbumImage = [[UIImage alloc] initWithData:responseData];
self.photo1ImageView.image = photoAlbumImage;
}];
[weakRequest setFailedBlock:^{
NSError *error = [request error];
NSLog(@"error geting file: %@", error);
}];
[weakRequest startAsynchronous];
}
我已经修改了ASIHTTPRequest
示例代码页中的示例代码,以消除Xcode中的编译器警告.
I've modified the sample code from the ASIHTTPRequest
example code page to eliminate compiler warnings in Xcode.
如何摆脱这些内存泄漏?我似乎只有在使用积木时才得到它们.
How can I get rid of these memory leaks? I only seem to get them when using blocks.
推荐答案
您从完成块内部引用了错误的请求变量.您应该在块中引用request
(这就是为什么使用__block
标识符声明它的原因).实际上,您根本不需要声明weakRequest
.
You're referencing the wrong request variable from inside the completion block. You should reference request
in the block (that's why you declare it with the __block
identifier). In fact, you shouldn't need to declare weakRequest
at all.
如果您希望将请求保留在内存中,请将其存储在您的类的@property (retain)
中(也许是使用buildPhotoView
方法的请求).
If you want the request to be kept in memory, store it in an @property (retain)
in your class (the one with the buildPhotoView
method perhaps).
这篇关于ASIHTTPRequest内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!