UIView 类中的内存泄漏

发布于 2024-12-04 06:32:36 字数 1385 浏览 0 评论 0原文

我已经使用以下代码创建了自定义 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;

}

I have created customize UIView with following code .....it's running with no issue....but when i check for Memory leaks...it shows leaks at point where i have made comment ...this is subclass of Uiview.
Please help me...

-(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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

匿名。 2024-12-11 06:32:36

试试这个

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

  [self setBackgroundColor:myColor];

  [myColor release];

嘿伙计,这不是内存泄漏。那是自动释放的对象。所以不要担心它们。苹果不会拒绝你的。条件是如果你使用alloc,init,new或retain,那么只有你有责任释放。所以离开它。否则就像上面的alloc和init一样释放它。就这样

try this

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

  [self setBackgroundColor:myColor];

  [myColor release];

Hey man that is not memory leaks. that is autoreleased objects.so dont worry about them.apple wont reject urs.the condition is if u use alloc,init,new or retain then only u have responsibility to release.so leave it.otherwise do like above by alloc and init then release it .that'st it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文