简体   繁体   中英

OBJ-C: How to release object which is return from method?

I am confused for the memory warning of object which is returning from method. This is my code.

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

}

This is my two methods. 1.-(void)returnHeaderView. 2.-(UILabel *)returnLabel.

-returnLabel is returning label and the reference of UILabel object which is returned by method is pass to the fromLabel UILabel object of returnHeaderView method. Then i release fromLabel object.

But it is giving a memory warning( in correct decrement of the reference count of an object that is not owned at this point by the caller).

So any one suggest me what is wrong in this code. And How to release object which is returned by method. 图像来自我的代码,它描述了通过分析程序生成的内存警告。

Thanks.

In your returnHeaderView you don't actually retain fromLabel , therefore you don't own it and shouldn't release it there (the warning "incorrect decrement of the reference count of an object that is not owned at this point by the caller").

The best option is to autorlease the returned label: return [label autorelease]; and let it live until the surrounding autorelease pool is drained. Note that this also means removing the [fromLabel release]; call from your returnHeaderView method.

If this sounds complicated, use ARC as it's actually recommended.

Please make small change and leak removes and also autorelease self.headerView to remove other memory leak warning:

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

It looks to me like your returnLabel method returns an object with a retain count of 1, not zero as the compiler is suggesting. I think you might just need to change the method name to give the compiler a hint about the ownership of the returned object. Try changing returnLabel to createReturnLabel . Then the method will conform to the copy/create naming convention .

You can avoid the leaks by changing your code like:

-(void)returnHeaderView
{
  self.headerView=[[UIView alloc]init];
  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];

  [fromLabel release];
  [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];

}

Suggestion: The best way to avoid such leaks is using ARC.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM