首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ASIHTTPRequest内存泄漏

ASIHTTPRequest内存泄漏
EN

Stack Overflow用户
提问于 2011-10-12 13:20:55
回答 1查看 2.8K关注 0票数 2

我有一个iOS项目,其中我在自己的类中使用了ARC,但在其他库中关闭了ARC,比如ASIHTTPRequest

使用下面的代码从web服务器获取图像时,我遇到了巨大的内存泄漏:

代码语言:javascript
复制
-(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中的编译器警告。

我怎样才能摆脱这些内存泄漏?我似乎只有在使用块时才能得到它们。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-10-12 13:42:57

您从完成块内部引用了错误的请求变量。应该在块中引用request (这就是为什么要用__block标识符来声明它)。实际上,您根本不需要声明weakRequest

如果希望将请求保存在内存中,请将其存储在类的@property (retain)中(可能是使用buildPhotoView方法的那个类)。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7735586

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档