选择器的内存泄漏? (苹果手机应用程序)
Posted
技术标签:
【中文标题】选择器的内存泄漏? (苹果手机应用程序)【英文标题】:memory leak with picker? (iphone App) 【发布时间】:2010-04-01 15:07:39 【问题描述】:我正在开发一个视图,该视图通过一个依赖选择器根据用户输入选择图像(图表),该依赖选择器使用一个 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];
【问题讨论】:
【参考方案1】:我觉得你不用打电话
[array release];
[selectedChartType release];
在您的 viewDidLoad 方法中,因为这两个对象都是自动释放的。也许这会导致崩溃。
【讨论】:
以上是关于选择器的内存泄漏? (苹果手机应用程序)的主要内容,如果未能解决你的问题,请参考以下文章