开发者

Release message to a UINavigationController object

开发者 https://www.devze.com 2023-01-08 08:32 出处:网络
I am relatively new to Cocoa programming, and some aspects of memory managing are still troubling me.

I am relatively new to Cocoa programming, and some aspects of memory managing are still troubling me.

In this case, I am creating a UINavigationController using the alloc message, and initialising it with a UIView controller. Then, I am presenting the view modaly by passing it to the presentModalViewController method. Below is the code:

- (void)tableView:(UITableView *)tableView 
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Tapped on disclosure button");
NewPropertyViewController *newProperty = [[NewPropertyViewController alloc] 
                                          initWithDictionary];
newProperty.editProperty = [fetchedResultsController objectAtIndexPath:indexPath];
UINavigationController *newPropertyNavigationController = [[UINavigationController 
                                          alloc] 
                                          initWithRootViewController:newProperty];
[newProperty setPropertyDelegate:self];
[self presentModalViewController:newPropertyNavigationController animated:YES];

[newProperty release];
[newPropertyNavigationController release];
}

According to the retain count rules, if I send the message "alloc" to a class, an instance of that class is returned with retain count 1, and I am responsible for releasing it. In the above code, I release the newPropertyNavigationController instance after passing it to the modalViewController and presenting it. When I dismiss the modal view, the app crashes.

If I comment out the last line, the app doesn't crash.

Why is this happening? Is t开发者_如何学Pythonhe particular alloc/init message to the UINavigationController working differently to the way it works in other classes, ie. is it perhaps returning an autoreleased instance?

Thanks!

Peter


You need to stop what you're doing and read through this. The rules for memory management are very simple and understandable. Let them be burnt into your head (doesn't take long). Then examine your code line by line in your trouble spots, and apis that get called into from your code. Tracing through your code this way while the rules are fresh in your head, will help you fix your memory problems, and maybe, just maybe, help you prevent from making them in the future.


The way you create the modal view controller looks correct. Check the implementation of dealloc on your modal view controllers to see if the problem lies there.

If you are incorrectly deleting memory there it would explain why you only get the error when you release the modal view controller.

For reference I find the following use of autorelease much more readable and maintainable

NewPropertyViewController *newProperty = [[[NewPropertyViewController alloc] 
                                      initWithDictionary] autorelease];
newProperty.editProperty = [fetchedResultsController objectAtIndexPath:indexPath];
UINavigationController *newPropertyNavigationController = [[[UINavigationController 
                                      alloc]
                                      initWithRootViewController:newProperty] autorelease];
[newProperty setPropertyDelegate:self];
[self presentModalViewController:newPropertyNavigationController animated:YES];
0

精彩评论

暂无评论...
验证码 换一张
取 消