我有一个iOS项目,其中我在自己的类中使用了ARC,但在其他库中关闭了ARC,比如ASIHTTPRequest。
使用下面的代码从web服务器获取图像时,我遇到了巨大的内存泄漏:
-(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中的编译器警告。
我怎样才能摆脱这些内存泄漏?我似乎只有在使用块时才能得到它们。
发布于 2011-10-12 13:42:57
您从完成块内部引用了错误的请求变量。应该在块中引用request (这就是为什么要用__block标识符来声明它)。实际上,您根本不需要声明weakRequest。
如果希望将请求保存在内存中,请将其存储在类的@property (retain)中(可能是使用buildPhotoView方法的那个类)。
https://stackoverflow.com/questions/7735586
复制相似问题