ASIHTTPRequest内存泄漏

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

我使用下面的代码从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中的编译器警告。

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

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

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