2012-03-03 54 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的,你不妨重新思考你是如何做