IPhone SDK - 使用performSelectorInBackground泄漏内存 [英] IPhone SDK - Leaking Memory with performSelectorInBackground

查看:107
本文介绍了IPhone SDK - 使用performSelectorInBackground泄漏内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许有人可以帮我解决这个奇怪的事情:

Maybe someone can help me with this strange thing:

如果用户点击按钮,新的UITableView会被推送到导航控制器。这个新视图正在进行一些数据库查询,这需要一些时间。因此我想在后台进行加载。

If a user clicks on a button, a new UITableView is pushed to the navigation controller. This new view is doing some database querying which takes some time. Therefore I wanted to do the loading in background.

什么有效而没有泄漏内存(但冻结屏幕直到一切都完成):

What works WITHOUT leaking memory (but freezes the screen until everything is done):

WorkController *tmp=[[WorkController alloc] initWithStyle:UITableViewStyleGrouped];
self.workController=tmp;
[tmp release];

[self.workController loadList]; // Does the DB Query
[self.workController pushViewController:self.workController animated:YES];  

现在我尝试这样做:

    // Show Wait indicator
    ....

    WorkController *tmp=[[WorkController alloc] initWithStyle:UITableViewStyleGrouped];
    self.workController=tmp;
    [tmp release];

    [self performSelectorInBackground:@selector(getController) withObject:nil];
}

-(void) getController {
    [self.workController loadList]; // Does the DB Query
    [self.navigationController pushViewController:self.workController animated:YES];
}

这也有效,但是内存泄漏,我不知道为什么!
你能帮帮忙吗?

This also works but is leaking memory and I don't know why ! Can you help ?

顺便说一下 - 应用程序是否可能因内存泄漏而进入AppStore?或者首先检查一下?

By the way - is it possible for an App to get into AppStore with a small memory leak ? Or will this be checked first of all ?

提前致谢!

推荐答案

不,小内存泄漏(很可能)你的应用程序不会被appstore拒绝。

No, small memory leaks will not (most likely) you application to be rejected from appstore.

在你的例子中,你在单独的线程中运行你的方法你应该为该线程创建并处置 NSAutoreleasePool 对象以处理自动释放的对象。对 getController 方法的更改应该可以解决问题:

In your example as you run your method in separate thread you should create and dispose NSAutoreleasePool object for that thread to handle autoreleased objects. Following changes to getController method should do the trick:

-(void) getController {
    NSAutoreleasedPool *pool = [[NSAutoreleasedPool alloc] init];

    [self.workController loadList]; // Does the DB Query
    [self.navigationController pushViewController:self.workController animated:YES];

    [pool release];
}

有关详细信息,请参阅 Autorelease Pools 部分。那里的相关引用:

For more details see Autorelease Pools section in memory management guide. Relevant quote from there:


如果你产生了一个辅助线程,你
必须创建你自己的自动释放池
as线程开始执行
;否则,你将泄漏
对象。 (参见Autorelease Pools和
Threads
了解详情。)

这篇关于IPhone SDK - 使用performSelectorInBackground泄漏内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆