ASIHTTPRequest 内存泄漏

发布于 2024-12-09 12:29:37 字数 1107 浏览 0 评论 0原文

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

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

I have a iOS project in which I am using ARC in my own classes, but have ARC turned off in other libraries like ASIHTTPRequest.

I'm getting huge memory leaks using the code below to fetch an image from a web server:

-(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];

}

I've modified the sample code from the ASIHTTPRequest example code page to eliminate compiler warnings in Xcode.

How can I get rid of these memory leaks? I only seem to get them when using blocks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说好的呢 2024-12-16 12:29:37

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

如果您希望将请求保留在内存中,请将其存储在类中的 @property (retain) 中(可能是带有 buildPhotoView 方法的类)。

You're referencing the wrong request variable from inside the completion block. You should reference request in the block (that's why you declare it with the __block identifier). In fact, you shouldn't need to declare weakRequest at all.

If you want the request to be kept in memory, store it in an @property (retain) in your class (the one with the buildPhotoView method perhaps).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文