废弃的记忆

时间:2013-01-24 10:55:58

标签: ios memory-management memory-leaks modalviewcontroller abandoned-memory

在我的代码中:

    ABNewPersonViewController *c = [[ABNewPersonViewController alloc] init];
    [c setNewPersonViewDelegate:self];
    [c setDisplayedPerson:newPerson];
    CFRelease(newPerson);


    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:c];
    [self presentModalViewController:nc animated:YES];
有些地方,它会成为一个废弃的记忆,我会检测到仪器。 有什么线索的原因?

2 个答案:

答案 0 :(得分:0)

发布c。它已保留在nc

答案 1 :(得分:0)

尝试删除CFRelease,并将UINavigationController设为ivar。这就是ARC知道将导航控制器保留在内存中直到特定类被释放的方式。

ABNewPersonViewController *personViewController = [[ABNewPersonViewController alloc] init];
[personViewController setNewPersonViewDelegate:self];
[personViewController setDisplayedPerson:newPerson];

//this line is not required
//CFRelease(newPerson); 

//UINavigationController *personNavigationController; define nc as ivar in the .h file.
personNavigationController = [[UINavigationController alloc] initWithRootViewController:personViewController];
[self presentModalViewController:personNavigationController animated:YES];

如果您想遵循Apple的一些命名约定,那么您的代码的可读性肯定会很棒。