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

发布于 2024-11-27 19:41:27 字数 143 浏览 2 评论 0原文

我有一个 MFMailComposeViewController 对象,我在电子邮件按钮的回调中释放它,只是因为我创建了它,而且我认为我这样做是间歇性的,但并不总是会使我的应用程序崩溃。

如何使用Xcode的检测程序来检测这种情况?

谢谢。

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.

How can I use Xcode's instrumentation program to detect such a situation?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千柳 2024-12-04 19:41:27

您可以将 NSZombieEnabled 环境变量设置为 YES产品 > 编辑方案...,选择运行(产品名称),单击参数选项卡并编辑环境变量列表)。使用NSZombie,对象不会被释放,而是变成僵尸。向它们发送消息会将错误记录到控制台,而不是因 EXC_BAD_ACCESS 导致崩溃。这样您就可以查明是否确实是 MFMailComposeViewController 造成了问题。

但保留和释放视图控制器可能甚至没有必要。如果您在创建 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];
}

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). With NSZombie, objects will not be deallocated but turned into zombies. Messaging them will log an error to the console instead of crashing with EXC_BAD_ACCESS. That way you can find out if it’s really the MFMailComposeViewController that’s causing trouble.

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:

- (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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文