简体   繁体   English

如何使用Xcode检测对象的双重释放?

[英]How to use Xcode to detect double release of object?

I've got a MFMailComposeViewController object that I'm releasing in my Email button's callback, simply because I created it, and I think my doing so is intermittently but not always crashing my app. 我有一个要在“电子邮件”按钮的回调中释放的MFMailComposeViewController对象,仅仅是因为我创建了它,我认为这样做虽然断断续续,但并不总是使我的应用程序崩溃。

How can I use Xcode's instrumentation program to detect such a situation? 如何使用Xcode的检测程序检测这种情况?

Thanks. 谢谢。

You can set the NSZombieEnabled environment variable to YES ( Product > Edit Scheme… , the select Run (Product Name) , click the Arguments tab and edit the list of environment variables). 您可以将NSZombieEnabled环境变量设置为YES产品>编辑方案… ,选择运行(产品名称) ,单击参数选项卡并编辑环境变量列表)。 With NSZombie , objects will not be deallocated but turned into zombies. 使用NSZombie ,对象将不会被释放而是变成僵尸。 Messaging them will log an error to the console instead of crashing with EXC_BAD_ACCESS . 向他们发送消息会将错误记录到控制台,而不是因EXC_BAD_ACCESS而崩溃。 That way you can find out if it's really the MFMailComposeViewController that's causing trouble. 这样一来,您就可以确定是否确实是MFMailComposeViewController引起了麻烦。

But retaining and releasing the view controller might not even be necessary. 但是甚至没有必要保留和释放视图控制器。 If you present the MFMailComposeViewController immediately after creating it and don't use it anymore after it's been dismissed, there's no need to retain it: 如果您在创建MFMailComposeViewController之后立即显示它,而在关闭它后不再使用它,则无需保留它:

- (IBAction)composeMessage:(id)sender {
    MFMailComposeViewController *mailComposeViewController = [[[MFMailComposeViewController alloc] init] autorelease];
    mailComposeViewController.mailComposeDelegate = self;
    [self presentModalViewController:mailComposeViewController animated:YES];
}

- (void)mailComposeController:(MFMailComposeViewController *)mailComposeViewController didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    // Present error to the user if failed
    [self dismissModalViewControllerAnimated:YES];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM