UIView子类,之前发布加载它?
问题描述:
我试图展现一个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;
}
答
loadNibNamed:
返回autorelease
倒是对象NSArray
。因此,当你从alloc/init
得到它时,你的OTChatMessageBox
是autorelease
'd。这意味着您的结尾release
正在导致过度销售。问题是init
方法应该返回一个调用者期望拥有所有权的对象。
self = [super init];
是一个内存泄漏,因为您从不使用返回的对象,也不会释放它,因为您已经释放它的对象已经释放。在这种情况下,你会需要像
self = [super init];
[self release];
... grab stuff from nib
当然这是一个不必要的alloc/init
的,你不妨重新思考你是如何做
我们可以看到'OTChatMessageBox' – 2012-03-04 00:07:02
确保'init'方法! :)但我认为是正确的吗? – NemeSys 2012-03-04 00:47:39