选择器内存泄漏? (iPhone 应用程序)

发布于 2024-08-28 02:53:35 字数 2805 浏览 5 评论 0原文

我正在开发一个视图,该视图通过依赖选择器根据用户输入选择图像(图表),该选择器使用一个 plist 文件,该文件包含三个数组,每个数组包含大约 40 个字符串。

大约 50% 的情况下,当我运行应用程序时,它会立即关闭,但在其他时候运行时,它会正常工作,直到我开始滚动选择器,此时它会在大约 4 秒后崩溃。它也不会在左侧组件上显示数组名称之一。

我尝试使用分析器运行应用程序来检查内存泄漏,但如果我在内存泄漏模式下使用分析器,它根本不会运行。代码几乎直接来自书本。

这是相关代码:

    -(void) viewDidLoad {
        [super viewDidLoad];
        UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"US High Index.jpg"]];
        self.imageView = tempImageView;
        NSString *string = @"US High Index";
        self.chartNameLabel.text = string;
        [string release];
        [tempImageView release];


    scrollView.contentSize = CGSizeMake(imageView.frame.size.width , imageView.frame.size.height);
    scrollView.maximumZoomScale = 4.0;
    scrollView.minimumZoomScale = .05;
    scrollView.clipsToBounds = YES;
    scrollView.delegate = self;

    [scrollView addSubview:imageView];

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *plistPath = [bundle pathForResource:@"ChartTypes" ofType:@"plist"];

    NSDictionary *dictionary = [[NSDictionary alloc]initWithContentsOfFile:plistPath];

    self.chartsDictionary   = dictionary;


    NSArray *components = [self.chartsDictionary allKeys];
    self.chartTypes = components;


    NSString *selectedChartType = [self.chartTypes objectAtIndex:0];
    NSArray *array = [chartsDictionary objectForKey:selectedChartType];
    self.charts = array;

    [dictionary release];
    [array release];

    [selectedChartType release];
    [plistPath release];
    [bundle release];
}



-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

    -(NSInteger)pickerView:(UIPickerView *)pickerView
    numberOfRowsInComponent:(NSInteger)component {
        if (component == kChartTypeComponent ) 
            return [self.chartTypes count];

            return [self.charts count];

    }

    #pragma mark Picker delegate methods
    -(NSString *)pickerView:(UIPickerView *)pickerView
    titleForRow:(NSInteger)row
               forComponent:(NSInteger)component {
        if (component == kChartTypeComponent ) 
            return [self.chartTypes objectAtIndex:row];

            return [self.charts objectAtIndex:row];



    }

    -(void)pickerView:(UIPickerView *)pickerView
          didSelectRow: (NSInteger)row
          inComponent:(NSInteger)component {
        if (component == kChartTypeComponent ) {
            NSString *selectedChartType = [self.chartTypes objectAtIndex:row];
            NSArray *array = [chartsDictionary objectForKey:selectedChartType];
            self.charts = array;
            [picker selectRow: 0 inComponent:kChartComponent animated: YES];
            [picker reloadComponent:kChartComponent];

        }
        }

I am working on a view that selects an image (chart) based on user input through a dependent picker which uses a plist file that contains three arrays containing about 40 strings each.

About 50% of the time when I run the app, it just shuts down immediately, but the other times that it does run, it will work fine until I start scrolling through the picker at which point it crashes after about 4 seconds. It also wont show one of the array names on the left component.

I tried running the app with the analyzer to check on a memory leak, but it wont run at all if I am using analyzer in memory leak mode. The code is almost straight out of a book.

Here is the relevant code:

    -(void) viewDidLoad {
        [super viewDidLoad];
        UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"US High Index.jpg"]];
        self.imageView = tempImageView;
        NSString *string = @"US High Index";
        self.chartNameLabel.text = string;
        [string release];
        [tempImageView release];


    scrollView.contentSize = CGSizeMake(imageView.frame.size.width , imageView.frame.size.height);
    scrollView.maximumZoomScale = 4.0;
    scrollView.minimumZoomScale = .05;
    scrollView.clipsToBounds = YES;
    scrollView.delegate = self;

    [scrollView addSubview:imageView];

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *plistPath = [bundle pathForResource:@"ChartTypes" ofType:@"plist"];

    NSDictionary *dictionary = [[NSDictionary alloc]initWithContentsOfFile:plistPath];

    self.chartsDictionary   = dictionary;


    NSArray *components = [self.chartsDictionary allKeys];
    self.chartTypes = components;


    NSString *selectedChartType = [self.chartTypes objectAtIndex:0];
    NSArray *array = [chartsDictionary objectForKey:selectedChartType];
    self.charts = array;

    [dictionary release];
    [array release];

    [selectedChartType release];
    [plistPath release];
    [bundle release];
}



-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

    -(NSInteger)pickerView:(UIPickerView *)pickerView
    numberOfRowsInComponent:(NSInteger)component {
        if (component == kChartTypeComponent ) 
            return [self.chartTypes count];

            return [self.charts count];

    }

    #pragma mark Picker delegate methods
    -(NSString *)pickerView:(UIPickerView *)pickerView
    titleForRow:(NSInteger)row
               forComponent:(NSInteger)component {
        if (component == kChartTypeComponent ) 
            return [self.chartTypes objectAtIndex:row];

            return [self.charts objectAtIndex:row];



    }

    -(void)pickerView:(UIPickerView *)pickerView
          didSelectRow: (NSInteger)row
          inComponent:(NSInteger)component {
        if (component == kChartTypeComponent ) {
            NSString *selectedChartType = [self.chartTypes objectAtIndex:row];
            NSArray *array = [chartsDictionary objectForKey:selectedChartType];
            self.charts = array;
            [picker selectRow: 0 inComponent:kChartComponent animated: YES];
            [picker reloadComponent:kChartComponent];

        }
        }

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

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

发布评论

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

评论(1

独﹏钓一江月 2024-09-04 02:53:35

我认为您不必调用

[array release];
[selectedChartType release];

viewDidLoad 方法,因为这两个对象都是自动释放的。也许这可能会导致崩溃。

I think you don't have to call

[array release];
[selectedChartType release];

in your viewDidLoad method, because both objects are autoreleased. Maybe that could cause the crash.

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