1

EDIT: Sorry for my question. It turned out that the error was in the line before the one that was flagged by the static analyzer.
In this line, I called a Obj-C method that creates and returns an ABRecordRef, and although I balanced the creation of the CF object by a CFRelease in the calling code, the static analyzer was apparently not sure if the memory management of the CF object was done correctly, thus the "potential leak".
I converted the method to a C function, and the warning is gone.

ORIGINAL QUESTION:
Under ARC, I have a subclass of a UINavigationController that is presented modally.
In the viewDidLoad method, I set up an ABNewPersonViewController, and present it by pushing it onto the navigation stack, using the following code:

ABNewPersonViewController *personViewController = [[ABNewPersonViewController alloc] initWithNibName:nil bundle:nil];
personViewController.newPersonViewDelegate = self;
personViewController.displayedPerson = self.contactToBeAdded;f
[self pushViewController:personViewController animated:NO];  

Everything works fine, but the static analyzer gives, at the 1st line of the code, the warning "Memory (Core Foundation/Objective-C) Potential leak of an object".
I don't understand why there is this warning, and how I could get rid of it.
Any suggestion?

Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116

1 Answers1

1

You try to present a new UIViewController from viewDidLoad and the problem is that this controller may not be added to UIWindow hierarchy.

Try to present this controller in viewDidAppear or viewWillAppear methods, it should fix the warning.

boweidmann
  • 3,312
  • 2
  • 20
  • 27
  • Yes, of course. [Here](http://stackoverflow.com/questions/16949353/uiviewcontroller-present) you can find a good answer of @Stas to the same question with related documentation. – boweidmann Jan 12 '14 at 09:12
  • Well yes, that only tells us what we know, that you should never present a view or viewController in the viewDidLoad delegate, as the view is not yet initialized, but this doesn't mean that your view or viewController will not added to the UIWindow view hierarchy. If a context can't be found to draw the new view or viewController you will get an error from the OS, such as the one you did show on your link. – Lefteris Jan 12 '14 at 09:36
  • That's right. Actually after `viewDidLoad` the view **is** initialized, but not yet attached to current view hierarchy. As you can read [from documentation](https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/presentViewController:animated:completion:), the `presentViewController` method add the view to view hierarchy, which may not be the current one. – boweidmann Jan 12 '14 at 09:56
  • @Bogdan Weidmann: Thank you for your answer. I tried your suggestion, but the warning remained. I then looked more closely to the code around that one above, and it turned out that the error was there. Sorry, I should have done this before. I will update my question accordingly. However, I do not understand your suggestion: What I did in viewDidLoad is putting the 1st view controller onto the stack of the navigation controller. Its view will be added to the view hierarchy only when the navigation controller is presented modally later on. I believe this is correct, or is it not? – Reinhard Männer Jan 12 '14 at 13:01
  • Yes, it's correct. I'm sorry, I misunderstood your question. I read `presentViewController` instead of `pushViewController`, which is a common problem if you call it from `viewDidLoad`. – boweidmann Jan 12 '14 at 14:35
  • @Bogdan Weidmann: Thanks again for your comments. OK, then the only problem was that I misinterpreted the yellow warning indicator in the line following the problematic one. – Reinhard Männer Jan 12 '14 at 15:31