memory leak with picker? (iphone App)
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";
开发者_StackOverflow 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 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.
精彩评论