2012-03-03 32 views
1

我试图展现一个UIView子类:UIView子类,之前发布加载它?

-(void)pushChatNewMessage:(id)sender 
{ 
    NSNotification *not = (NSNotification *)sender; 
    NSNumber *num = (NSNumber *)not.object; 


    OTChatMessageBox *chatMessageBox = [[OTChatMessageBox alloc] init]; 

    chatMessageBox.frame = CGRectMake(123, 60, 778, 208); 

    chatMessageBox.toId = [num intValue]; 

    [UIView beginAnimations:@"animationChatBox" context:nil]; 
    [UIView setAnimationDuration:0.5]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:chatMessageBox cache:YES]; 

    [self.view addSubview:chatMessageBox]; 

    [UIView commitAnimations]; 

    [chatMessageBox release]; 

} 

的问题是,我得到这个错误:

modifiying layer that is being finalized 

我在调试观察到OTChatMessageBox对象的dealloc方法被称为只是结束这种方法。

如果我删除的对象的发布,一切工作正常...有一个伟大的泄漏...

我回顾OTChatMessageBox的init()方法是绝对简单,只有一个TextView对象,并用一个按钮通知电话。

我失踪了什么?

预先感谢;)

- 编辑 -

-(id)init 
{ 
    self = [super init]; 

    if (self) 
    { 
     self = [[[NSBundle mainBundle] loadNibNamed:@"OTChatMessageBox" owner:self options:nil] objectAtIndex:0]; 

     [txtMessage becomeFirstResponder]; 
    } 

    return self; 
} 
+0

我们可以看到'OTChatMessageBox' – 2012-03-04 00:07:02

+0

确保'init'方法! :)但我认为是正确的吗? – NemeSys 2012-03-04 00:47:39

回答

1

loadNibNamed:返回autorelease倒是对象NSArray。因此,当你从alloc/init得到它时,你的OTChatMessageBoxautorelease'd。这意味着您的结尾release正在导致过度销售。问题是init方法应该返回一个调用者期望拥有所有权的对象。

self = [super init];是一个内存泄漏,因为您从不使用返回的对象,也不会释放它,因为您已经释放它的对象已经释放。在这种情况下,你会需要像

self = [super init]; 
[self release]; 

... grab stuff from nib 

当然这是一个不必要的alloc/init的,你不妨重新思考你是如何做