需要帮助修复iPhone内存泄漏!

时间:2022-11-06 07:19:52

I encountered some strange memory leaks executing following code on iPhone device:

我在iPhone设备上执行以下代码时遇到了一些奇怪的内存泄漏:

@implementation TestViewController
@synthesize myButton;

- (IBAction)buttonPressed {
    ABPeoplePickerNavigationController* selectContactViewController = nil;

    selectContactViewController = [[ABPeoplePickerNavigationController alloc] init];
    selectContactViewController.peoplePickerDelegate = self;
    [self presentModalViewController:selectContactViewController animated:YES];
    [selectContactViewController release];
}  

Releasing the picker simple done as follows:

发布简单的选择器如下:

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {

    [self dismissModalViewControllerAnimated:YES];
}

Instruments marks "selectContactViewController = [[ABPeoplePickerNavigationController alloc] init];" as leaking. Any idea why?

仪器标记“selectContactViewController = [[ABPeoplePickerNavigationController alloc] init];”泄漏。知道为什么吗?

2 个解决方案

#1


0  

You might want to construct your Picker control like so:

你可能想构建你的Picker控件,如下所示:

ABPeoplePickerNavigationController* selectContactViewController = nil;

selectContactViewController = [[[ABPeoplePickerNavigationController alloc] init] autorelease];
selectContactViewController.peoplePickerDelegate = self;
[self presentModalViewController:selectContactViewController animated:YES];

When you present the modal view controller, it will retain the view on its own. That's how it's able to still pass you an instance of the view controller to your delegate. Best bet is to set the view controller to be autoreleased, so when it gets popped from the navigation controller, the NSAutoReleasePool will garbage collect it.

当您呈现模态视图控制器时,它将自己保留视图。这就是它如何能够将视图控制器的实例传递给您的委托。最好的办法是将视图控制器设置为自动释放,因此当它从导航控制器弹出时,NSAutoReleasePool将对其进行垃圾收集。

#2


0  

Just a comment - do you use any protocol like UINavigationControllerDelegate in the interface declaration?

只是一个评论 - 您是否在接口声明中使用任何协议,如UINavigationControllerDelegate?

I encountered a situation where just referencing this protocol caused a similar leak message.

我遇到一种情况,只是引用此协议导致类似的泄漏消息。

#1


0  

You might want to construct your Picker control like so:

你可能想构建你的Picker控件,如下所示:

ABPeoplePickerNavigationController* selectContactViewController = nil;

selectContactViewController = [[[ABPeoplePickerNavigationController alloc] init] autorelease];
selectContactViewController.peoplePickerDelegate = self;
[self presentModalViewController:selectContactViewController animated:YES];

When you present the modal view controller, it will retain the view on its own. That's how it's able to still pass you an instance of the view controller to your delegate. Best bet is to set the view controller to be autoreleased, so when it gets popped from the navigation controller, the NSAutoReleasePool will garbage collect it.

当您呈现模态视图控制器时,它将自己保留视图。这就是它如何能够将视图控制器的实例传递给您的委托。最好的办法是将视图控制器设置为自动释放,因此当它从导航控制器弹出时,NSAutoReleasePool将对其进行垃圾收集。

#2


0  

Just a comment - do you use any protocol like UINavigationControllerDelegate in the interface declaration?

只是一个评论 - 您是否在接口声明中使用任何协议,如UINavigationControllerDelegate?

I encountered a situation where just referencing this protocol caused a similar leak message.

我遇到一种情况,只是引用此协议导致类似的泄漏消息。