gpt4 book ai didi

ios - 使用 performSelectorInBackground 时内存泄漏

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:15:27 25 4
gpt4 key购买 nike

我是 iOS 开发新手。我在运行后台线程时遇到问题。在我的代码中,resetUi 在主 UI 线程上运行,现在我正在启动一个后台线程来获取图像数据并更新我的图像。一切正常,但在调用 performSelectorInBackground 时内存泄漏。

请告诉我哪里做错了。另外请建议是否有更好的方法在从 URL (dataWithContentsOfURL) 获取图像时更新我的​​图像。

[更新]

仪器显示 2 个单独的泄漏,一个在 perfromSelectorInBackground,另一个在 UIImage imageWithData。我猜 imageupdate(imageWithData) 出现了严重错误

-(void)updateData{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

profileName.text = oAuthTwitter.screen_name;

if(profilePic.image == nil){
NSString *urlString = @"https://api.twitter.com/1/users/profile_image/";
urlString = [urlString stringByAppendingFormat:oAuthTwitter.screen_name];
urlString = [urlString stringByAppendingFormat:@"?size=bigger"];
profilePic.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]];
[activityIndicator stopAnimating];
[activityIndicator release];
}
[pool drain];

}

- (void)resetUi{

if (oAuthTwitter.oauth_token_authorized) {
profilePic.hidden = NO;
profileName.hidden = NO;

NSLog(@"Resetting to authorised state");
[self performSelectorInBackground:@selector(updateData) withObject:nil];

}else{

NSLog(@"Resetting Twitter UI to non-authorized state.");

profilePic.hidden = YES;
profileName.hidden = YES;

}

}

最佳答案

我认为你应该使用

[pool release];

而不是

[pool drain];

这是更好的做法。

还有可以尝试在主线程中释放activityIndi​​cator吗?

从您提供的代码中我找不到任何其他泄漏原因。您是否尝试过使用泄漏工具和静态分析器运行您的代码?

关于ios - 使用 performSelectorInBackground 时内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10811019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com