简体   繁体   中英

“[CALayer release]: message sent to deallocated instance” when dismissing modal view controller

I've been struggling with this for last few days and I cannot find any solution, so I ask you for advice.

I have two UIViewControllers: NewPostUIViewController and SettingsUIViewController. In the second one I have a field:

id<SettingsUIViewControllerDelegate> delegate

and the first one implements protocol

SettingsUIViewControllerDelegate

When a button is pressed the following code is executed in NewPostUIViewController:

SettingsUIViewController *settingsUIViewController  = [[SettingsUIViewController alloc] initWithNibName:@"SettingsView" bundle:nil]; 
settingsUIViewController.title = NSLocalizedString(@"Settings", @"Settings view title");
settingsUIViewController.delegate = self; 
[self presentModalViewController:settingsUIViewController animated:YES];
[settingsUIViewController release];

when I want to dismiss SettingsUIViewController I call (code in SettingsUIViewController):

[delegate settingsAreDone:sender];

and settingsAreDone looks following (code in NewPostUIViewController):

    [self dismissModalViewControllerAnimated:YES];

This all concludes in:

[CALayer release]: message sent to deallocated instance 0x5a76840

I tried to debug the code by setting a breakpoint in the release methods of both view controllers, but these methods are called so often that it's hard to say what can be the cause of this problem.

Any ideas?

First, the error you're getting isn't indicating that -release is being sent to a view controller, so breakpoints in your view controllers won't help. The over-release is happening on a CALayer, which is likely part of the modal animation.

First, we start with some basics about the delegate. I don't feel great about this being the cause, but you should always start with the easy basics. Your SettingsUIViewController delegate property should be assign, not retain, so you avoid retain loops. That's probably correct already, but when it's not, you can wind up with cases where objects exist longer than you expect them to (and so can send messages after their targets have gone away). Again, probably not the issue, but easy to check and easy to fix.

Next, you should look at the stack trace at the crash. Who is calling [CALayer release] ? A possible cause is that the owning view controller gets released before the animation stops. When you close the settings controller, do you immediately close the NewPost controller?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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