开发者

memory release problem

I have created a view and assigned the view to viewcontroller

UIView *newView=[[UIView alloc] initWithFrame:CGRectMake(0,0,320,460)];

in viewDidLoad method I have assigned the view to the viewcontroller

-(void)viewDidLoad{
  开发者_开发问答  self.view=newView;
    //[view release]; In this case also the application crashing
}

-(void)dealloc{
   [newView release];//IN this case also the application crashing.
   [super dealloc];
}

The crash log is this.

memory release problem

how to release the newView? or else the viewcontroller itself will take care of releasing the newView.


In most circumstances you will do the following:

- (void) loadView {
    // Don't call super here if you assign the view property
    UIView *newView = [[UIView alloc] initWithFrame: [UIScreen mainScreen].applicationFrame];
    self.view = newView;
    [newView release];
    // the view is now owned and retained by the UIViewController
}

Apple recommends you allocate the view, assign and release it in -loadView and don't worry about it in -dealloc. This is important in cases when your view may be purged under low memory conditions.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜