iOS4 - fast context switching

≡放荡痞女 提交于 2019-12-01 17:15:35

You might still have your UIViewControllers still retained if dealloc isn't being called.

Perhaps are you setting delegates or other classes in these UIViewControllers that retained and referenced back up the tree (circular references).

A way you can debug this is to overload retain and release in your UIViewController and set a break point and log the retainCount.

Here is a magic snippet I leave running around that helps me a ton when I can't figure out why I'm still retaining something.

- (id)retain
{
    NSLog(@"retain \t%s \tretainCount: %i", __PRETTY_FUNCTION__ , [self retainCount]);
    return [super retain];
}
- (void)release
{
    NSLog(@"release \t%s \tretainCount: %i", __PRETTY_FUNCTION__ , [self retainCount]);
    [super release];
}
- (id)autorelease
{
    NSLog(@"autorelease \t%s \tretainCount: %i", __PRETTY_FUNCTION__ , [self retainCount]);
    return [super autorelease];
}

__PRETTY_FUNCTION__ is a special hidden macro in CLang that gives a pretty Objective-C function name as a char array.

  1. When an iOS starts running out of memory it tries to kill the background processes that are using the most memory. So while there's no absolute good number, minimising how much memory you use is a good idea. Leaving it at 30Mb is tantamount to guaranteeing that your app will be killed
  2. Unless you want to change your UI there is no need to use anything other that a UINavigationController to deal with your back button. I think the problem here is that if dealloc is not called on a pop or dismiss, you have a memory leak

Almost all view controllers have data that is effectively cached and can be regenerated when the app returns to the foreground. Think of the data that you release when you get a memory warning when the app is running. (You are responding to memory warnings, right?) It's that stuff that should be released when you go into the background.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!