简体   繁体   中英

Memory Management in iPhone?

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

See the mothod, After doing Product > Analyze in XCode, I will get the following warning at line number

return retval;

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

Let me know, what is this, how would I do release,

Please edit this code, and explain me what have you changed in it, Thanks

retval is an UILabel that you allocate but never release. Usually, you would create it as

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

and in the calling method, add it to a view or whatever.

Just when you allocate the label, keep it in autorelease mode, as you'll need to release RETVAL somewhere. You haven't released it, hence a leak is found.

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

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