UINavigationController: app freezes - ios

In myAppDelegate.m:
MainViewController *mainViewController = [ [MainViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:mainViewController];
[navController setNavigationBarHidden:YES];
[[self window] setRootViewController:navController];
In MainViewController.m nothing special, just one action tied to a button:
- (IBAction)go:(id)sender {
if (!whereamiViewController)
{
whereamiViewController = [[WhereamiViewController alloc] init];
}
[[self navigationController] pushViewController:whereamiViewController animated:YES];
}
And in WhereamiViewController.m just a button to show another screen in UINavigationViewController:
-(IBAction)showList:(id)sender
{
PointsViewController *container = [[PointsViewController alloc] init];
[[self navigationController] pushViewController:container animated:YES];
}
And:
#interface PointsViewController : UITableViewController
#end
Everything fine. But when I land on the last screen, PointsViewController, and I go back on WhereamiView, the app freeze, and Xcode shows a trap at CoureFoundation, CFHas.
Now, I know that Objective-C is not php, but this is a simple example indeed: what am I missing? There is a method to debug the problem?
The debugger says:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [__NSCFType _forgetDependentConstraint:]: unrecognized selector sent to instance
This morning I ran the app inside the simulator and no crashes!! How is it possible? If I run inside the iPhone crash, in the simulator no crash.

Tim is right. Popping the PointsViewController is the way I would do it, too.
What does your Xcode saying? Can you provide the content of the debugger?
That would be more helpful to track down what's going wrong.

[PARTIALLY SOLVED]
This is very strange: After debugging everything and deleting every suspicious declaration, nothing improved. Same crash SIGABRT. After rebooting both iPhone and Mac, everything is fine!

I am not really seeing where the problem is as it occurs outside of the code you have posted. I may be missing something, but if the app crashes when you go back to whereamiViewController, how do you actually go back? I am not seeing where it's detailed here.
With navigationControllers like this, you would push (to add) or pop (to remove) viewControllers to move up and down the navcontroller tree. As it is at this point that the app seems to be crashing, it may be worth postings how you are dong this? Where are you popping the container (PointsViewController *).
From the code you've posted, I suspect you may be trying to move back to the whereamiViewController by re-pushing it again.

Related

crash with ios8, popToViewController and UIViewControllerHierarchyInconsistency

Everything was fine with iOS7 and Xcode and 5 - and after the upgrade to Xcode 6 and so iOS8, there is a sudden crash in the application where it was always working fine.
The error shown when it crashes is:
Terminating app due to uncaught exception
'UIViewControllerHierarchyInconsistency', reason: 'adding a root view
controller as a child of view
controller:'
I managed to find the single line creating the issue:
[self.navigationController
popToViewController:[self.navigationController.viewControllers
objectAtIndex:4] animated:YES];
The app adds views up to 8 whilst doing the exercise ; and when the exercise is done, just coming back to the last view which was the menu of the exercise so 4. A bit hard-coded but simple and efficient as it is always the case.
and I have no idea was the popToViewController is doing that.
Any help or thoughts would be greatly appreciated.
I finally found a solution that works - so here it is before for completion / records:
UINavigationController* savedUinvc = self.navigationController;
UIViewController *one = nil;
one = [savedUinvc popViewControllerAnimated:NO];
UIViewController *two = nil;
two = [savedUinvc popViewControllerAnimated:NO];
UIViewController *three = nil;
three = [savedUinvc popViewControllerAnimated:YES];

EXC_BAD_ACCESS (code 2) when calling 'presentViewController'

I'm really stumped here.
_vc = [[VLCKitViewControlleriPhone alloc]initWithNibName:#"VLCKitViewControlleriPhone" bundle:nil];
_vc.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:_vc animated:YES completion:nil];
Gives me EXC_BAD_ACCESS (code = 2, address = 0x0) when the presentViewController method is called. The view controller is NOT nil. It also happens with or without a nib name. If I comment out the presentViewController line, the rest of the code continues fine, including method calls made to the view controller itself. The view controller is running, I just can't see anything because it's not actually showing the view.
I enabled NSZombies and tried it with Instruments running, but it's not showing me anything. The app just quits and instruments stops without giving me any information. Anyone know what the problem might be?
You can try this
if ([controller respondsToSelector:#selector(setModalPresentationStyle:)])
{
[controller setModalPresentationStyle:UIModalPresentationFullScreen];
} else {
[controller setModalPresentationStyle:UIModalPresentationFormSheet];
}

UIAlertView cancel button crashes app

Okay, so I have just started iOS development. I'll start by explaining the flow of my app :
1. A view called "appViewController" is loaded.
2. [self presentViewController: controller animated: YES completion:nil]; this loads a webview
3. After I am done with the webview, I dismiss it and load a new UINavigation this way :
[self dismissViewControllerAnimated:YES completion:^{
formViewController *fv = [ [formViewController alloc] init ];
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:fv] autorelease];
navController.navigationBar.hidden = YES;
[presentingViewController presentModalViewController:navController animated:YES];
}];
5.The formViewController has a button, which has the event attached to it for that display an alert this way
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"Oops!"
message:#"test"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[av show];
Everything works as intended up till here. Now when I click the "Ok"(Cancel) button, the app crashes with NSZombieEnabled saying
-[appViewController _deepestDefaultFirstResponder]: message sent to deallocated instance 0x6e6a570 lldb
What is happening here? Why is it trying to send message to appViewController again? There is no code after [av show]
NOTE : I am using ARC
If you're using arc, the autorelease in your code isn't valid.
This seems like your root view controller gets deallocated at some point and when the responder chain gets traversed the resulting dangling pointer gets accessed.
To verify this, I would implement the dealloc method on appViewController and see if it gets called.
dealloc {
NSLog(#"Problems ahead.");
}
If this does get called before you'd expect that to happen (for a root view controller probably not at all), you need to find out why this happens. You're probably missing a strong reference somewhere. Check you app delegate and verify that you have a strong reference to the window and that you are setting your controller as the root view controller (provided you're not using storyboards).
The Zombies instrument is very good for debugging such problems. It will list all retains and releases of your problematic object. Here's a short introduction to it.
A couple of things.
You are mixing presentViewController and presentModalViewController. If you are new to iOS, you should always use presentViewController. No need getting used to using a method that will soon be deprecated (see answer to this question Difference between presentModalViewController and presentViewController?
In general, you shouldn't autorelease unless you absolutely have to. Since when a controller is presented with presentViewController (and presentModalViewController), it is retained, you can readily release afterwards. I would restructure like so:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:fv];
navController.navigationBar.hidden = YES;
[presentingViewController presentModalViewController:navController animated:YES];
[navController release];
3.. Where is the section of code you included located? Like, you push a button and the view is dismissed or what?

UIBarButtonItem makes my iPhone app crash

I'm working on a project and I'm trying to do the most I can programmatically.
I've to add an UIBarButtonItem to a NavigationController's nav bar created in the App Delegate.
WPViewController *mainVC = [[WPViewController alloc] initWithNibName:#"WPViewController_iPhone" bundle:nil];
UINavigationController *navCon = [[UINavigationController alloc] init];
[navCon pushViewController:mainVC animated:NO];
[self.window addSubview:navCon.view];
Then in the implementation file of the here declared WPViewController I create and add the barbuttonitem as a navigation item of the VC:
UIBarButtonItem *rBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(test)];
self.navigationItem.rightBarButtonItem = rBarButtonItem;
There is a method called test declared before of this that simply log "test", but when I click on the button the app crashes.
Please help me, this bug is driving me crazy.
Notes:
I'm using ARC in my project
Never had a similar bug before
" Message sent to deallocated instance" in ARC means the compiler has marked, and released, your item before your message could be sent.
Set NSZombieEnabled, MallocStackLogging, and guard malloc in the debugger. Then, when your App crashes, type this in the console:
(gdb) info malloc-history //address of crashing object i.e. 0x543216//
I also had this problem when using addSubview but creating a property with (nonatomic, strong) strong solved it for me.
The Button tries to pass itself as an argument to the test method. I guess your signature of that method doesn't include an argument, because there's no colon in your selector (it should be #selector(test:)). And the method implementation should look like:
- (void) test:(id)sender

What is wrong with this code? Trying to load a navController onto a modal view controller

I have a UIViewcontroller that I want to push onto a UINavigationController, which in turn would push onto a modal view using presentModalViewController:animated.
Here's my code:
TargetViewController *targetViewController = [[[TargetViewController alloc] init] autorelease];
UINavigationController *targetNavController = [[[UINavigationController alloc] initWithRootViewController:targetViewController] autorelease];
[self presentModalViewController:targetNavController animated:YES];
When code is run, the modal view loads as expected, but after dismissModalViewControllerAnimated: is called, the modal view slides down and the app crashes.
I get the following error in gdb:
-[CALayer retain]: message sent to deallocated instance
First part of question: is there anything inherently wrong with the above code?
Second part: if there is nothing wrong with above code, where should I look next to debug?
Additional info:
When I don't release or autorelease the navController, it works fine. But Instruments will show abandoned memory, which I can only assume is the navController not being released. Maybe the modal view controller
P.S. I know that the crash is related to the memory management of the above ViewController, navController and modal view, because my code was working prior to messing with this code.
You generally don't push a navController as a view as the navigation controller works as the root controller. The views are pushed from the navController. Once you have a view pushed, you could then present the next view modally.
What happens if you don't use autorelease?
I.E.:
TargetViewController *targetViewController = [[TargetViewController alloc] init];
UINavigationController *targetNavController = [[UINavigationController alloc] initWithRootViewController:targetViewController];
[targetViewController release];
[self presentModalViewController:targetNavController animated:YES];
[targetNavController release];
Fixed the issue. I was releasing the VC, causing the crash. Thanks for the input.

Resources