Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c ASIHTTPRequest内存泄漏_Objective C_Ios_Asihttprequest - Fatal编程技术网

Objective c ASIHTTPRequest内存泄漏

Objective c ASIHTTPRequest内存泄漏,objective-c,ios,asihttprequest,Objective C,Ios,Asihttprequest,我有一个iOS项目,其中我在自己的类中使用ARC,但在其他库中禁用了ARC,如ASIHTTPRequest 我使用下面的代码从web服务器获取图像时出现了巨大的内存泄漏: -(void)buildPhotoView { self.photoLibView.hidden = NO; NSString *assetPathStr = [self.cellData objectForKey:@"AssetThumbPath"]; // get the thumbnail image of the

我有一个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中的编译器警告


我怎样才能消除这些内存泄漏?我似乎只有在使用积木的时候才能得到它们

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


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

使用自动删除对象实例化
photoAlbumImage
是否会减小泄漏的大小?i、 e.
photoAlbumImage=[UIImage imageWithData:responseData]使用自动引用计数时不能使用自动释放。当我在不使用块的情况下使用ASIHTTPRequest时,我不会出现内存泄漏,但我需要这样做,因为我正在对图像执行多个请求,每个请求都进入tableCell中不同的UIImageView。对于块,我可以在请求中包含一个完成块,在请求完成时将图像放入正确的UIImageView。我声明weakRequest是因为Xcode显示了一个警告:自动引用计数问题:在该块中强烈捕获“请求”可能会导致一个保留周期。当我不声明weakRequest.Oh时,我会得到相同数量的错误。在这种情况下,前缀
request
\uuuuu unsafe\u unrepaired
以及
\uuuu block
。我尝试使用:
\uuuuu block\uuuuuuunsafe\u unrepaired ASIHTTPRequest*request
,它在调试模式下工作,但在生产中出现崩溃!-问题是,在启用objective c优化时,一旦您尝试访问
请求
,此代码就会崩溃,因此如果它是对请求对象的唯一引用,请不要同时使用
\uuuuuuuuunsecreted\uuuuuuuuuu block
)您真的不应该再使用
ASIHTTPRequest
。它早就被弃用了,任何人都不支持它,甚至最初的开发人员也不支持它。