is this lead to memory leak for iphone - 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];

Related

presenting a view not working in iphone

I am trying to navigate from one page to another on a button click in a facebook appliaction.
I am using
registerUser=[[RegisterPage alloc]initWithNibName:#"RegisterPage" bundle:nil];
[self presentModalViewController :registerUser animated:YES];
for presenting the next view after getting response from facebook.
But it is not showing the next view. It works fine in all other places where I used to present other views.
Anyone have idea about this issue?
What exactly is 'self' here? Is it a viewcontroller? Or just a UIView?
I think this'll only work if self is a viewcontroller or some subclass of it.
My code to present a view controller is somewhat like yours (without a nib):
ViewController *controller = [[ViewController alloc] initWithNibName:nil bundle:nil];
[controller setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:controller animated:YES];
[controller release];
And to present a navigation controller it's like this (without a nib):
ViewController *controller = [[ViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
[navController setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:navController animated:YES];
[navController release];
[controller release];
Sometimes simply copying someones code helps.
thanks for everyones reply.i have solved it after a long fight.i just dismissed the view before pesenting the next view and set dismissmodalviewcontrollerAnimated to NO.[self dismissModalViewControllerAnimated:NO];
nextview = [[LoginPage alloc]initWithNibName:#"LoginPage" bundle:nil];
[self presentModalViewController: nextview animated:YES];hope this help someone like me

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.

pushViewController and Release

I am trying to do like this, with the commented lines it works good, without, when I came back from the "pushed" view my App just crashes... when should I [release]? Or, better, I am doing this correctly?
if (indexPath.row == 1) {
Credits *cr = [[Credits alloc] initWithNibName:#"Credits" bundle:nil];
[self.navigationController pushViewController:cr animated:YES];
//[cr release];
}else{
Search *sr = [[Search alloc] initWithNibName:#"Search" bundle:nil];
[self.navigationController pushViewController:sr animated:YES];
//[sr release];
}
You should release your controller after pushing it onto the navigation view controller's stack of controllers. My guess is that something else is going on in the dealloc of your Search and Credits objects, that you are overreleasing an object there.

How to do a login screen?

I have this working but I don't think it is working correctly so I just wanted to get your feedback. I am trying to display a screen that has two buttons - one that takes you to a login screen and the the allows you to register.
II am testing in the appDelegate if they are logged in and if they aren't I am showing the signLogIN view.
signLogIN = [[LoginOrSignUPViewController alloc] init];
signLogIN.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
signLogIN.tabBarController = mainAPPTabBarController;
signLogIN.mainWindow = window;
[window addSubview:[signLogIN view]];
//[signLogIN release];
(I release in the appDelegate dealloc - if I release here it blows up when they select to either login or register).
I did try doing:-
[mainAPPTabBarController presentModalViewController:signLogIN animated:NO];
But it made no difference.
Curiously I can see that the dealloc in LoginOrSignUPViewController is called straight away - why is that? I can't tell where it is being called from.
From LoginOrSignUPViewController I am then displaying the login screen by doing:-
[self retainCount] = 1
LoginViewController *logINVC = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
logINVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
logINVC.delegate = self;
logINVC.tabBarController = self.tabBarController;
[self presentModalViewController:logINVC animated:YES];
[logINVC release];
now [self retainCount] = 3 = why did it go to three????
As you can see there is a delegate that calls back to the signLogIn view to close the view as follows:-
[self retainCount] = 3
[mainWindow bringSubviewToFront:tabBarController.view];
tabBarController.selectedIndex = 0;
[self.view removeFromSuperview];
[self dismissModalViewControllerAnimated:NO];
[self release];
[self retainCount] = 3 -- still 3 it never goes away
So here is my main problem LoginOrSignUPViewController never goes away it just sits behind my main view. The only line that makes any difference is the [mainWindow bringSubViewToFront. Does anyone have any ideas as to how to make the LoginOrSignUPViewController disappear?
Thanks very much
Cheryl
Have you tried this in your view controller's viewDidLoad:
LoginOrSignUPViewController *signLogIN = [[LoginOrSignUPViewController alloc] init];
[self presentModalViewController:signLogIN animated:NO];
[signLogIn release];

Stack overflow by presentModelViewController in 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.