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 中的编译器警告。
我怎样才能摆脱这些内存泄漏?我似乎只在使用块时得到它们。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您从完成块内部引用了错误的请求变量。您应该在块中引用
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 declareweakRequest
at all.If you want the request to be kept in memory, store it in an
@property (retain)
in your class (the one with thebuildPhotoView
method perhaps).