ASIHTTPRequest内存泄漏

Alp*_*sta 2 objective-c asihttprequest ios

我有一个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];
Run Code Online (Sandbox Code Playgroud)

}

我已经修改了示例代码ASIHTTPRequest页中的示例代码,以消除Xcode中的编译器警告.

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

dar*_*s0n 7

您正在完成块内引用错误的请求变量.您应该request在块中引用(这就是您使用__block标识符声明它的原因).实际上,你根本不需要申报weakRequest.

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

  • 哦.在这种情况下,前缀`request`与`__unsafe_unretained`以及`__block`. (2认同)