我很困惑从方法返回的对象的内存警告. 这是我的代码. -(void)returnHeaderView{ self.headerView=[[UIView alloc]init]; headerView.frame=CGRectMake(0, 0, 955, 45); UILabel *fromLabel=[self returnLabel]; fromLabel.frame=CGRec
这是我的代码.
-(void)returnHeaderView { self.headerView=[[UIView alloc]init]; headerView.frame=CGRectMake(0, 0, 955, 45); UILabel *fromLabel=[self returnLabel]; fromLabel.frame=CGRectMake(400, 5, 200, 44); fromLabel.text=@"Open Time"; [headerView addSubview:fromLabel]; [fromLabel release];(in correct decrement of the reference count of an object that is not owned at this point by the caller) [self.headerView addSubview:fromLabel]; [self.view addSubview:self.headerView]; [self.headerView release];
}
-(UILabel *)returnLabel { UILabel *label= [[UILabel alloc] init] ; label.textColor = [UIColor blackColor]; label.font = FONT_TITLE; label.numberOfLines=1; label.textAlignment=UITextAlignmentLeft; label.lineBreakMode=UILineBreakModeWordWrap; label.backgroundColor=[UIColor clearColor]; return label; }
这是我的两种方法.
1 .-(无效)returnHeaderView.
2 .-(UILabel *)returnLabel.
-returnLabel返回标签,方法返回的UILabel对象的引用传递给returnHeaderView方法的fromLabel UILabel对象.
然后我从Label对象中释放.
但它给出了记忆警告(
正确减少调用者此时不拥有的对象的引用计数.
所以任何人都建议我这个代码有什么问题.
以及如何释放方法返回的对象.
谢谢.
请进行小的更改并删除泄漏,并自动释放self.headerView以删除其他内存泄漏警告:-(void)returnHeaderView { self.headerView=[[[UIView alloc]init] autorelease]; headerView.frame=CGRectMake(0, 0, 955, 45); UILabel *fromLabel = [[self returnLabel] retain]; fromLabel.frame = CGRectMake(400, 5, 200, 44); fromLabel.text = @"Open Time"; [headerView addSubview:fromLabel]; [self.headerView addSubview:fromLabel]; [self.view addSubview:self.headerView]; //[self.headerView release]; } -(UILabel *)returnLabel { UILabel *label= [[UILabel alloc] init] ; label.textColor = [UIColor blackColor]; label.font = FONT_TITLE; label.numberOfLines=1; label.textAlignment=UITextAlignmentLeft; label.lineBreakMode=UILineBreakModeWordWrap; label.backgroundColor=[UIColor clearColor]; return [label autorelease]; }