2010-12-08 60 views
-1

我已创建的视图并分配该视图的viewController存储器释放问题

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

在viewDidLoad方法我已分配的视图到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]; 
} 

崩溃日志是这样的。 alt text

如何释放newView?否则viewcontroller本身会照顾释放newView。

+0

您发布的代码似乎并不完整 - 您在哪里分配newView?几乎可以肯定的是,在将它分配给self.view之后释放newView - 查看Cocoa内存管理指南(http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html) – 2010-12-08 14:04:07

回答

0

在大多数情况下,你会做到以下几点:

- (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 
} 

苹果建议你分配来看,分配和-loadView释放和-dealloc不要担心。这在您的视图可能在内存不足的情况下被清除的情况下非常重要。