UIPickerView seems to be leaking each time I rotate a component

亡梦爱人 提交于 2019-12-11 05:33:40

问题


UPDATE:

Seems to me like it is pretty much an Apple bug. I tried the following:

  1. create a new single-window project
  2. create a single UIPickerView like the one shown below, only that the picker just allows for turning the dials. That is, neither variables nor any state is manipulated when interacting the picker.

RESULT: No matter whether I use the simple titleForRow:forComponent or viewForRow:forComponent, the picker still leaks 48 bytes each time a dial is turned. Further, I even tried having the picker return views previously allocated in an array set up as a class property in the viewController, so that no views are retained or released but for whatever the picker might do internally after or before it calls the delegate methods. And still, the leaks occur.

Seems like an Apple bug to me.

ORIGINAL QUESTION

I have a textField whose imputView is set to a UIPickerView with 4 components. The picker is used to select a weight / mass which becomes the text in the textField.

Under Instruments, each time I turn a dial / component in the UIPickerView, a leak occurs:

Here's the stack trace for the 48 byte leak:

The code through which the UIPickerView gets each component view is:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    if(!view)
    {
        view = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 37)] autorelease];
        UILabel *label = (UILabel*)view;
        label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        label.textAlignment = UITextAlignmentCenter;
        label.font = [UIFont boldSystemFontOfSize:24];
        label.backgroundColor = [UIColor clearColor];
    }

    ((UILabel*)view).text = [NSString stringWithFormat:@"%@%i", ((component == 3) ? @"." : @""), row];
    return view;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return  60;
}

And the code that updates the textField to the new textual value is:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSString *newWeight = [NSString stringWithFormat:@"%i%i%i.%i", 
                           [pickerView selectedRowInComponent:0], 
                           [pickerView selectedRowInComponent:1],
                           [pickerView selectedRowInComponent:2],
                           [pickerView selectedRowInComponent:3]];

    self.sampleMassBuffer = [NSDecimalNumber decimalNumberWithString:newWeight];
    weightTextField.text = [[numberFormatter stringFromNumber:self.sampleMassBuffer] stringByAppendingFormat:@" %@", currentConfiguration.weightUnits];
}

I have no idea how the leaks are originating or if they are even mine! Could it be an Apple bug?


回答1:


I'm not sure if this question is answerable any more, except to say that you should file a bug report with Apple using your basic example project.




回答2:


The leak in the SDK. I just confirmed with a trusted source.



来源:https://stackoverflow.com/questions/9790243/uipickerview-seems-to-be-leaking-each-time-i-rotate-a-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!