开发者

- (void)dealloc Question

开发者 https://www.devze.com 2023-02-01 22:37 出处:网络
Can you tell me if the following code is 100% correct? Expecially the dealloc section FirstViewController.h

Can you tell me if the following code is 100% correct? Expecially the dealloc section

FirstViewController.h

#import <UIKit/UIKit.h>
#import "SecondViewController.h"

@class SecondViewController

@interface FirstViewController : UIViewController
{
    SecondViewController   *SecondController;
}

- (IBAction)SwitchView;

@property (nonatomic, retain) IBOutlet SecondViewController *Second开发者_如何学编程Controller;

@end

FirstViewController.m

#import "FirstViewController.h"

@implementation FirstViewController

@synthesize SecondController;

- (IBAction)SwitchView
{    
    SecondController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    SecondController.modalTransitionStyle = UIModalPresentationFullScreen;
    [self presentModalViewController:SecondController animated:YES];
    [SecondController release];
}

/// OTHER CODE HERE ///

- (void)dealloc
{
    [SecondController release];
    [super dealloc];
}

@end

Thanks!


No it is not correct. You are sending the release message to a pointer in dealloc, but the pointer may or may not point to the SecondController anymore. This may lead to some very weird bugs, typically random objects being released.

In objective-c terms, your class doesn't retain (think "own") the SecondController, so it should not try to release it in the first place on dealloc.

To claim and release ownership the correct way, make it so:

- (IBAction)SwitchView
{    
    self.SecondController = [[[SecondViewController alloc] 
                  initWithNibName:@"SecondViewController" bundle:nil] autorelease];
    self.SecondController.modalTransitionStyle = UIModalPresentationFullScreen;
    [self presentModalViewController:self.SecondController animated:YES];
}

/// OTHER CODE HERE ///

- (void)dealloc
{
    self.SecondController = nil;
    [super dealloc];
}

This will also protect you from any other stuff happening between SwitchView and dealloc. (as long as that stuff follows the rules and uses self.SecondController = ... to change the property)

In SwitchView the alloc/autorelease sequence makes that your routine keeps ownership for the length of the routine (and a little beyond). The self.SecondController = part makes sure that your class retains the SecondController object, since you declared it (nonatomic,retain).


You should use the property setter to assign SecondController.

I suggest you only alloc/init that view controller once, then in SwitchView show it:

#import "FirstViewController.h"

@implementation FirstViewController

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle {
   if((self = [super initWithNibName:nibName bundle:nibBundle])) {
      self.SecondController = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];
      SecondController.modalTransitionStyle = UIModalPresentationFullScreen;
   }
   return self;
}

- (IBAction)SwitchView
{    
    [self presentModalViewController:SecondController animated:YES];
}

/// OTHER CODE HERE ///

- (void)dealloc
{
    [SecondController release];
    [super dealloc];
}

@end

This way, you only actually create that SecondController view controller once, as opposed to creating it every time -SwitchView is invoked.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号