I made a multiview app based on this Tutorial.Here is my code in the appdelegate.h
@class Disclaimerviewcontroller;
@interface GAINSAppDelegate : NSObject <UIApplicationDelegate> {
Disclaimerviewcontroller *firstview;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain )Disclaimerviewcontroller *firstview;
-(void)switchview :(UIView *)view1 toview:(UIView *)view2;
@end
and in the “.m” i have the following
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
Disclaimerviewcontroller *aview = [[Disclaimerviewcontroller alloc]initWithNibName:@"Disclaimerviewcontroller"bundle:nil];
self.firstview = aview;
[_window addSubview:aview.view];
[aview release];
return YES;
}
-(void)switchview :(UIView *)view1 toview:(UIView *)view2{
[UIView beginAnimations:@"Animation" context:nil];
[UIView setAnimationDuration:0.50];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.window cache:YES];
[view1 removeFromSuperview];
[_window addSubview:view2];
[UIView commitAnimations];
}
and the dealloc
- (void)dealloc
{ [firstview release];
[_window release];
[super dealloc];
}
from there on when ever I want to switch to another view I use the following code
-(IBAction)switchtodisclaimer2:(id)sender{
GAINSAppDelegate *delegate = (GAINSAppDelegate *)[[UIApplication sharedApplication]delegate];
Disclaimerviewcontrller2 *disclaimview2 = [[Disclaimerviewcontrller2 alloc]initWithNibName:@"Disclaimerviewcontrller2" bundle:nil];
[delegate switchview:self.view toview:disclaimview2.view];
}
just like in the tutorial. but when I did an analyze test. xcode warned be of possible memory leak in the above code. so i added (in the above case) [disclaimerview2 release];and when I run the program I get EX_BAD_ACCES error. I thought since it was initwithnibname it was an autorelease ?. I’m confused now. the tutorial doesn’t seem to be addressing this at all.
The rule for memory ownership is this :
So yes, you do have a memory leak so you should release ‘disclaimview2’ with
[disclaimview2 release]However, the reason that you are crashing is because of another bug somewhere in your code.
If you are going to pass it into your delegate, your delegate should retain it – otherwise, the delegate will try to use it at some point in the future but will have been deallocated.