0

I have two controllers. In the first one, there is a buttn and when it is clicked, the second one will be shown by the presentModalViewController method. However, when I go back to the first one, the dismissModalViewController is called and the second controller is dissmissed but memory is not released. I used instrument to observe memory allocations and arc.

I thought I might did something wrong but I tried the official sample code, iPhoneCoreDataRecipes and CoreDataBooks, this also happend. I am wondering why the momery was not released?

Following is the code for presentModal and dismissModal from the official sample recipes:

- (void)add:(id)sender {
    RecipeAddViewController *addController = [[RecipeAddViewController alloc] initWithNibName:@"RecipeAddView" bundle:nil];
addController.delegate = self;

Recipe *newRecipe = [NSEntityDescription insertNewObjectForEntityForName:@"Recipe" inManagedObjectContext:self.managedObjectContext];
addController.recipe = newRecipe;

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addController];
    [self presentModalViewController:navigationController animated:YES];

    [navigationController release];
    [addController release];
}

- (void)recipeAddViewController:(RecipeAddViewController *)recipeAddViewController didAddRecipe:(Recipe *)recipe {
    if (recipe) {
         // Show the recipe in a new view controller
        [self showRecipe:recipe animated:NO];
    }

    // Dismiss the modal add recipe view controller
    [self dismissModalViewControllerAnimated:YES];
}

I did similar things but I used arc instead so I do not have release lines.

Wain
  • 118,658
  • 15
  • 128
  • 151
user2053760
  • 1,673
  • 2
  • 14
  • 19

1 Answers1

0

You might make retain cycles in your modal view controller. Retain cycles it is when first object has strong reference to second object which also has strong reference to first object. Read about retain cycles in apple documentation:

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-1000810

and here:

http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html (Use Lifetime Qualifiers to Avoid Strong Reference Cycles)

Vitaly Berg
  • 875
  • 8
  • 10
  • The thing is why the official samples have the same problem? could you have a look at sample iPhoneCoreDataRecipes or CoreDataBooks? I did similar to the first one. – user2053760 Apr 13 '13 at 07:41
  • You convert iPhoneCoreDataRecipes to ARC? – Vitaly Berg Apr 13 '13 at 07:47
  • No I did not, I simply run the sample and check the memory allocation. – user2053760 Apr 13 '13 at 07:54
  • iPhoneCoreDataRecipes don't use arc. Please show kind of your leaks. – Vitaly Berg Apr 13 '13 at 07:58
  • If you see strdup leak. You should not worry about it because it iOS bug with scrollview objects, see here this problem: http://stackoverflow.com/questions/9880336/obj-c-memory-leak-of-malloc-48-bytes-in-strdup-frame – Vitaly Berg Apr 13 '13 at 08:04