iPhone中的内存管理?

时间:2011-11-16 06:09:31

标签: iphone memory-management

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {

UILabel *retval = (id)view;

if (!retval) {
    retval= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
}

NSDictionary *destination = [appDelegate.destinations objectAtIndex:row];
retval.text = [destination objectForKey:@"name"];
retval.font = [UIFont systemFontOfSize:18];
return retval;
}

见mothod,After doing Product>在XCode中分析,我将在行号

处收到以下警告
return retval;

Potential leak of an object allocated on line 213 and stored into 'retval'

请告诉我,这是什么,我将如何发布,

请编辑此代码,并解释一下您对其进行了哪些更改, 感谢

2 个答案:

答案 0 :(得分:2)

retval是您分配但永不释放的UILabel。通常,您可以将其创建为

retval= [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)] autorelease];

并在调用方法中,将其添加到视图或其他任何内容。

答案 1 :(得分:1)

当您分配标签时,请将其保持在自动释放模式,因为您需要在某处释放RETVAL。你没有释放它,因此发现了泄漏。

retval= [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)]autorelease];