我在iPhone设备上执行以下代码时遇到了一些奇怪的内存泄漏:
@implementation TestViewController
@synthesize myButton;
- (IBAction)buttonPressed {
ABPeoplePickerNavigationController* selectContactViewController = nil;
selectContactViewController = [[ABPeoplePickerNavigationController alloc] init];
selectContactViewController.peoplePickerDelegate = self;
[self presentModalViewController:selectContactViewController animated:YES];
[selectContactViewController release];
}
如下所述简单地发布选择器:
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
[self dismissModalViewControllerAnimated:YES];
}
仪器标记“selectContactViewController = [[ABPeoplePickerNavigationController alloc] init];”泄漏。知道为什么吗?
答案 0 :(得分:0)
你可能想要像这样构建你的Picker控件:
ABPeoplePickerNavigationController* selectContactViewController = nil;
selectContactViewController = [[[ABPeoplePickerNavigationController alloc] init] autorelease];
selectContactViewController.peoplePickerDelegate = self;
[self presentModalViewController:selectContactViewController animated:YES];
当您呈现模态视图控制器时,它将自己保留视图。这就是它如何能够将视图控制器的实例传递给您的委托。最好的办法是将视图控制器设置为自动释放,因此当它从导航控制器中弹出时,NSAutoReleasePool将对其进行垃圾收集。
答案 1 :(得分:0)
只是评论 - 您是否在接口声明中使用任何协议,如UINavigationControllerDelegate?
我遇到的情况是,引用此协议会导致类似的泄漏消息。