Stack overflow by presentModelViewController in iphone - iphone

I'm still facing the problem when I launch my application in iPhone.
It shows the stack over flow by presentModelViewController because I'm using number of viewcontroller and calling the same viewcontroller from other viewcontroller but it gets terminated. Here I'm showing the code which I'm using in whole program to call other view controller
UV_AlarmAppDelegate *app7 = (UV_AlarmAppDelegate *)[[UIApplication sharedApplication]delegate];
[self presentModalViewController:app7.frmComparisionChartLink animated:YES];
[app7 release];
Still I'm releasing the pointer but still my app gets terminated.

You shouldn't release the app delegate. In short, unless you alloc, copy or retain an object you don't need to release it.

Apple's documentation shows modalPresentation done like this;
UIViewController *uiViewController = [[UIViewController alloc] initWithNibName:#"UIViewController" bundle:nil];
uiViewController.delegate = self;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:uiViewController];
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
[uiViewController release];
Hopefully that helps.

Related

Can't size controller when launching RedLaser overlay view controller

I am using the RedLaser SDK. My app is a split view. I'm trying to launch a RedLaser overlay when a barbutton is pressed on the master view controller.
The method gets called when the button is pressed and that's where the problems start. I have 3 different versions of the code that launches the overlay. Each has it's own problem.
Option 1
This was my baseline and I knew it wouldn't work because I hadn't initialized the overlay. The following code launches the view controller correctly but (obviously) doesn't do what I want.
// Working Code that brings up dialog but doesn't start camera overlay
SRSScanVINViewController *scanVINViewController y= [[SRSScanVINViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:scanVINViewController];
[navController setModalPresentationStyle:UIModalPresentationFormSheet];
[navController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentViewController:navController animated:YES completion:nil];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
Option 2
This code initiates the overlay controller and launches it without crashing. The overlay is working and the camera is active. The problem is that the viewcontroller/overlay is taking the whole screen. My controls (buttons, etc) are all layed out as if the view controller is taking a portion of the upper-left portion of the screen. This would work if I could get the overlay to be sized correctly.
// Working code that shows the overlay (camera on) but the overlay takes the whole screen
SRSScanVINViewController *scanVINViewController = [[SRSScanVINViewController alloc] init];
[pickerController setOverlay:scanVINViewController];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:scanVINViewController];
[navController setModalPresentationStyle:UIModalPresentationFormSheet];
[navController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentViewController:pickerController animated:YES completion:nil];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
Option 3
This attempt was to fix the problems in option 2 (above). Here is the code:
SRSScanVINViewController *scanVINViewController = [[SRSScanVINViewController alloc] init];
[pickerController setOverlay:scanVINViewController];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:pickerController];
[navController setModalPresentationStyle:UIModalPresentationFormSheet];
[navController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentViewController:pickerController animated:YES completion:nil];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
It crashes with the following error (nslog):
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <SRSMasterViewController: 0x1f59f540>.'
*** First throw call stack:
(0x37ecb88f 0x331fc259 0x30d86441 0x7f6b5 0x7edf7 0x37e253fd 0x30cbfe07 0x30d855e7 0x37e253fd 0x30cbfe07 0x30cbfdc3 0x30cbfda1 0x30cbfb11 0x30cc0449 0x30cbe92b 0x30cbe319 0x30ca4695 0x30ca3f3b 0x3630522b 0x37e9f523 0x37e9f4c5 0x37e9e313 0x37e214a5 0x37e2136d 0x36304439 0x30cd2cd5 0x7deb5 0x7de50)
terminate called throwing an exception
Any help would be greatly appreciated. Thanks!
I got the code to work but still have a couple of issues. Here's the code:
SRSScanVINViewController *scanVINViewController = [[SRSScanVINViewController alloc] init];
[pickerController setOverlay:scanVINViewController];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:scanVINViewController];
[navController setModalPresentationStyle:UIModalPresentationFormSheet];
[navController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
pickerControllerPopover = [[UIPopoverController alloc] initWithContentViewController:pickerController];
[pickerControllerPopover setDelegate:self];
[pickerControllerPopover setPopoverContentSize:CGSizeMake(320.0f, 460.0f)];
[pickerControllerPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}
else
{
[self presentViewController:pickerController animated:YES completion:nil];
}
This sets the size of the popover to the same size as my original controller (defined in the nib). I still have a couple of issues though.
The controller has 4 buttons in a button bar at the bottom. It also has a UIImage. The first time I load this controller, the buttons (and button bar and UIImage) are either missing are located in strange places. If I dismiss the popover controller by touching the screen somewhere outside of the controller and load the controller again, the controls are all in the right places. In fact, they are in the right places every time EXCEPT the first time.
Any ideas?

iPhone - Loading a new view and a deallocated object

So I feel like a serious rookie right now, but I have a problem I can't seem to figure out. I have a barebones app, with literally nothing in it except a login screen and a second view containing a tableview. When I add the second view after logging in (I have done this like 4 times before...), the table view goes through its delegates and appears that it's going to load, but something happens. I have enabled my NSZombies, and it appears to be deallocating the new view, right before it appears.
After tracing through it, and building up again piece by piece, it appears to happen after I wire the table to the view as the datasource/delegate in IB. I have set the view as a UITableViewDelegate, and the methods indeed get fired. Does anyone have any idea what might be causing this behavior?
Have you added the 'second'view to an exisitng view using addSubview: or added it to some form of UINavigationController or UITabBarController? When you do this it will automatically increase the retain count and whatever code you have releasing the view won't cause is to be deallocated.
In my AppDelegate application:didFinishLaunchingWithOptions I have something like;
LoginViewController *login = [[LoginViewController alloc] init];
[login setDelegate:self];
loginNavController = [[UINavigationController alloc]
initWithRootViewController:login];
[window addSubview:[loginNavController view]];
And then once login has occured (and succeeded using a protocol/delegate to send the message back to AppDelegate) I call this code;
UIViewController *newView1 = [[UIViewController alloc] init];
UIViewController *newView2 = [[UIViewController alloc] init];
UIViewController *newView3 = [[UIViewController alloc] init];
myTabBarController = [[UITabBarController alloc] init];
myNavController = [[UINavigationController alloc]
initWithRootViewController:newView1];
// nav controller now retaining
[newView1 release];
NSArray *viewControllers = [NSArray arrayWithObjects:myNavController,
newView2,
newView3,
nil];
[myTabBarController setViewControllers:viewControllers animated:YES];
[[myTabBarController view] setFrame:[[UIScreen mainScreen] applicationFrame]];
[window addSubview:[tabBarController view]];
// tab bar controller now retaining
[newView2 release];
[newView3 release];
// remove login from application
[[loginNavController view] removeFromSuperview];
The AppDelegate has the following declared in the header file;
LoginViewController *loginViewController;
UITabBarController *myTabBarController;
UINavigationController *myNavController;
In the dealloc method for the AppDelegate these are released.
This gives me my login page and then when that has processed my views with a top nav all controlled using the bottom tab bar.
Hope this helps in some way.
You have either too many release (or autorelease) calls - or not enough retain calls - in your view loading/transitioning code, but it's impossible to be more specific without seeing that code.
What's probably happening is the autorelease pool is being flushed between your view loading and your view being shown, and that's what's leading the behaviour you describe.

switch between xibs on button action

I'm having difficulty in switching between xibs :(
first i was using navigationController and it worked well but i want to do it without using navigationController.
I tried presentModalViewController but it crashes my application. :((
Here is the code :
myViewController *viewController = [myViewController alloc] initWithNibName:nil bundle:nil];
myViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController: myViewController animated:YES];
[myViewController release];
It is not working, ERROR received :
GDB:Program received signal : "EXC_BAD_ACCESS"
myViewController *viewController = [myViewController alloc] initWithNibName:nil bundle:nil];
viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController: viewController animated:YES];
this should work. your problem was that you declared a view controller and named it "viewController", but you were not using that. you were using the view controllers name, not the declared instance.
You have set your nibName to nil
Enter the name of your Xib file you want to display there
Like
myViewController *viewController = [[myViewController alloc] initWithNibName:#"nameOfYourXIB" bundle:nil]autorelease];
myViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController: myViewController animated:YES];
remove the release statement from ur code and everything will work fine.
Actually u r releasing the viewcontroller before it is shown as the superview.
//[myViewController release];
Hope this helps u...
Don't release he controller its a global variable i suppose so just make it in nil in dealloc

is this lead to memory leak for iphone

Let me explain in detais
In appDidfinish()
{
preLoginNavController = [[PreLoginNavController alloc] initPreLoginNavController];
[window addSubview:[preLoginNavController view]];
}
then in preLoginViewController when user press a button
then i am doing this to go to view2
RootViewController *arootController= [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:arootController animated:YES];
if i do this [arootController release]; then i cant come form view 2
now in view 2 when back button is pressed
then i am doing this
[self.navigationController popToRootViewControllerAnimated:YES];
so i cannot release [arootController release] else when i go to back view app quits with no error
and i need a prelogin view before Rootview thats why i did like that now what should i do .. my app is working fine but i want to fix that leak :(
HEY
i am getting this message when i click back button in view 2 after push and release in preLogin(1st view)
objc[408]: FREED(id): message release sent to freed object=0x466a340
I think yes, is leaking arootController once you pop it.
yeah, there will have a leak. 2 suggested solutions are:
[arootController autorelease];
or after you do :
[self.navigationController pushViewController:arootController animated:YES];
you can release it.
A good practice is that : who increase the retain Count, should decrease it. and because aRootController is init in that class, it should be released there
Edit:
This should be the correct code if you want to use navigationController:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
self.preLoginNavController = [[[PreLoginNavController alloc] init] autorelease];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.mainItemListViewController];
[window addSubview:[self.navigationController view]];
[window makeKeyAndVisible];
}
then when you need to push:
[self.navigationController pushViewController:anotherViewController animated:YES];

presentModalViewController not working properly

I have 3 views.
I want to do the following:
A presents B modally
A dismisses B
A presents C modally
I have setup a delegate pattern where A is B's delegate. This is how I am presenting and dismissing in B:
[delegate dismissB]; //this is just [self dismissModalViewControllerAnimated:NO]
[delegate presentC]; //this is just [self presentModalViewController:c animated:NO];
For some reason my app crashes when I execute this code with no debugger results (I have NSZombieEnabled).
When I comment out [delegate presentC] the app will dismiss B properly. When I comment out [delegate dismissB] the app does nothing, even though the line executes. I am not sure why?
UPDATE:
Here is the code in A
-(void)showARView{
[self dismissModalViewControllerAnimated:NO];
ARViewController* arViewController = [[[ARViewController alloc] initWithNibName:#"ARViewController" bundle:nil]autorelease];
UINavigationController *arNavController = [[UINavigationController alloc] initWithRootViewController:arViewController];
LeaderBoardTableViewController* lbViewController = [[[LeaderBoardTableViewController alloc] initWithNibName:#"LeaderBoardTableViewController" bundle:nil]autorelease];
lbViewController.title = #"Leaderboard";
UINavigationController *lbNavController = [[UINavigationController alloc] initWithRootViewController:lbViewController];
arTabBarController = [[UITabBarController alloc] init];//initWithNibName:nil bundle:nil];
arTabBarController.delegate = self;
arTabBarController.viewControllers = [NSArray arrayWithObjects:arNavController, lbNavController, nil];
arTabBarController.selectedViewController = arNavController;
[arNavController release];
[lbNavController release];
[self presentModalViewController:arTabBarController animated:NO];
}
Here is the code in B
[delegate showARView];
When you call dismissB the delegate dismisses the viewcontroller. Under normal circumstances (if you do not retain it elsewhere) this leads to the viewcontroller to be dealloced synchronously. And afterwards you are trying to access the delegate instancevariable, but for this the code needs a sane (hidden) self pointer, which is dealloced. I'm not sure if NSZombie can help in this case. You can easily find out if this is the reason for your crash, by inserting [[self retain] autorelease]; before [delegate dismissB];. However this is a hack and not a fix. You have a design problem.
This is not the way delegates are meant to be used. B presents some userinterface and receives some user interaction. It should then tell A what has happened via a delegate message, e.G. bWasCanceled or bFinished. It is the duty of the delegate, in your case A, to decide what to do next. So in your case the delegate may decide to dismiss B and instead present C. Or in code:
// Inside A
- (void)controllerB:(UIViewController*)ctl didFinishWithResult:(id)something {
[self dismissModalViewControllerAnimated:NO];
// Instantiate and initialize c
[self presentModalViewController:c animated:NO];
}
// Inside B
[delegate controllerB:self didFinishWithResult:#"OK"];
If I completely misinterpreted your code, and everything is fine there, I have an other suggestion. I have seen strange issues when dismissing and presenting modal viewcontrollers in the same round of the runloop. You may try [delegate performSelector:#selector(presentC) withObject:nil afterDelay:0.0]; and see if that helps.