【问题标题】:Memory leaks in UIView classUIView 类中的内存泄漏
【发布时间】:2011-09-12 10:43:07
【问题描述】:

我已经使用以下代码创建了自定义 UIView .....它运行没有问题....但是当我检查内存泄漏时...它在我发表评论的地方显示泄漏...这是Uiview 的子类。 请帮帮我...

-(id) initWithLabel:(NSString*)labelName{

self = [super init];
if (self) {
    self.layer.cornerRadius=8.0f;
    self.layer.borderColor=[UIColor whiteColor].CGColor;
    self.layer.borderWidth=2.0f;
/*memory leaks 28.6% */ [self setBackgroundColor:[UIColor colorWithRed:51.0/255 green:51.0/255 blue:51.0/255 alpha:1]];
    [self setFrame:CGRectMake(86, 168, 160, 77)];
    [self setAlpha:1];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 28, 75, 21)];
    [label setBackgroundColor:[UIColor  clearColor]];
    [label setTextAlignment:UITextAlignmentLeft];
    [label setTextColor:[UIColor whiteColor]];
    [label setFont:[UIFont fontWithName:@"Helvetica" size:17]];//memory leak 57%
    [label setText:labelName];
    [label setOpaque:NO];//memory leak
    [self addSubview:label];
    [label release];

    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(108, 22, 37, 37)];
    [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [activityIndicator setBackgroundColor:[UIColor clearColor]];
    [activityIndicator setHidesWhenStopped:YES];
    [self addSubview:activityIndicator];
    [activityIndicator release];
}
return self;

}

【问题讨论】:

    标签: iphone memory-management memory-leaks


    【解决方案1】:

    试试这个

      UIColor *myColor = [[UIColor alloc] initWithRed:51.0/255 green:51.0/255 blue:51.0/255 alpha:1];
    
      [self setBackgroundColor:myColor];
    
      [myColor release];
    

    嘿,这不是内存泄漏。那是自动释放的对象。所以不要担心它们。苹果不会拒绝 urs。条件是如果你使用 alloc、init、new 或保留,那么只有你有责任释放。所以离开它。否则通过 alloc 和 init 执行上述操作放开就行了

    【讨论】:

    • AppleVijay:thanx for answer...现在内存在第一点没有泄漏,但在接下来的两点(即字体)仍显示泄漏
    • 嘿伙计,这不是内存泄漏。那是自动释放的对象。所以不要担心它们。苹果不会拒绝 urs。条件是如果你使用 alloc、init、new 或保留,那么只有你有责任释放。所以离开它。否则通过 alloc 和 init 执行上述操作释放它。就是这样
    • 知道为什么分析将自动释放的对象显示为内存泄漏?
    猜你喜欢
    • 1970-01-01
    • 2010-11-19
    • 1970-01-01
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多