简体   繁体   English

解除ModalViewController时执行EXEC_BAD_ACCESS

[英]EXEC_BAD_ACCESS when Dismissing ModalViewController

I'm using a pretty standard recipe for presenting ModalViewControllers in my iPhone apps, but I've run across a situation where the recipe is broken and I'm confused. 我正在使用一个非常标准的配方在我的iPhone应用程序中展示ModalViewControllers,但是我遇到了一种情况,即配方被破坏了,我很困惑。 This is how I (pretty much always) set up the presentation: 这是我(几乎总是)设置演示文稿的方式:

MatcherViewController *controller = [[MatcherViewController alloc] initWithNibName:@"MatcherView" bundle:nil];
[controller setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[controller setDelegate:self];
[self presentModalViewController:controller animated:YES];
[controller release];

This always works great until I added one thing to the mix, and I sent a message to the new controller object before I presented it, like so: 直到我将一件事添加到混合中,并且在呈现它之前,我已经向新的控制器对象发送了一条消息,这一直很好,就像这样:

MatcherViewController *controller = [[MatcherViewController alloc] initWithNibName:@"MatcherView" bundle:nil];

[controller setPrimary:primaryIndex andSecondary:secondaryIndex];

[controller setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[controller setDelegate:self];
[self presentModalViewController:controller animated:YES];
[controller release];

Adding this method call seems to work until I dismiss the view controller...at which point the app crashes with an EXEC_BAD_ACCESS signal. 在我关闭视图控制器之前,添加此方法调用似乎是可行的...此时,应用程序因EXEC_BAD_ACCESS信号而崩溃。 I can get it to work with the extra line if I remove [controller release] , but then I'm afraid that will cause a leak. 如果删除[controller release] ,我可以使它与额外的行一起使用,但是恐怕会导致泄漏。 Any ideas why sending a message to the object prior to presentation would cause this? 有什么想法为什么要在演示之前向对象发送消息会导致这种情况? Is there a better way to pass simple parameters up to the ModalViewController? 有没有更好的方法将简单参数传递给ModalViewController?

Thanks for your time in straightening out the newbie ;p 感谢您抽出宝贵时间整理新手; p

在控制器的dealloc方法中,确保您没有释放过多的内容。

I'm going to guess that primaryIndex and secondaryIndex are improperly retained objects such that they depend on the modal view retaining them to survive. 我会猜测primaryIndexsecondaryIndex 不正确保留的对象 ,因此它们依赖于保留它们才能生存的模式视图。 When you release the modal view, they die but are then called somewhere else in the code causing the crash. 当您释放模式视图时,它们会死掉,但随后在代码中的其他地方调用会导致崩溃。

If they are retained properties of the class, always access them with the "self.propertyName" construction to make sure their retain counts are properly managed. 如果它们是该类的保留属性,请始终使用“ self.propertyName”构造访问它们,以确保正确管理其保留计数。

In my experience premature optimization in the form of over releasing is a major cause of Objective-C headaches today. 以我的经验,过分释放形式的过早优化是当今Objective-C头痛的主要原因。 Old school Objective-C coders were paranoid about leaks because they were almost impossible to track down by hand back in the day. 老派的Objective-C编码人员对泄漏感到疑虑,因为他们几乎不可能在一天之内手动找到泄漏。 That is why a lot of the resources still put so much emphasis on preventing leaks as you go. 这就是为什么很多资源仍然非常重视防止泄漏的原因。 However, with modern analyzing tools, leaks are usually trivial to track down. 但是,使用现代分析工具,通常很难发现泄漏。

During initial development, when in doubt don't release. 在最初的开发过程中,如有疑问,请不要发布。

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

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