iPhone SDK: Memory leak on picker
I have created a picker for my users to pick from a list of countries. The problem is that repeated opening and closing of the picker is resulting in “EXC_BAD_ACCESS” error. I suspect it might be a memory leak but I am not sure. I was hoping someone could shed some insight into why this might be happening?
//data source for UIPicker
NSArray *arrayCountryChoices;
arrayCountryChoices = [[NSArray alloc] initWithObjects:@"TK=TOKELAU",
@"TJ=TAJIKISTAN",
@"TH=THAILAND",
@"TG=TOGO",
@"TF=FRENCH SOUTHERN TERRITORIES",
@"GY=GUYANA",
@"TD=CHAD", nil];
//opening the picker
CountryViewController *countryVC = [[CountryViewController alloc] initWithNibName:@"CountryView" bundle:nil];
countryVC.delegate = self;
[self presentModalViewController:countryVC animated:YES];
[countryVC release];
//here is where I grab the data
//close country selector
[self dismissModalViewControllerAnimated:YES];
//parse out code
NSString *strCode = [chosenCountry substringToIndex:2];
//set the gui
txtCountry.开发者_JS百科text = strCode;
I think it may be because I am trying to release the Country Selector before the delegate has a chance to get its data? Also I am wondring if I should not release the picker until the screen that calls it is released.
Thanks in advance.
Releasing the view after you've added it as a modal view is not a problem because it's being held in the viewController's modalViewController property. However, when you dismiss the modal view it does die so this message:
NSString *strCode = [chosenCountry substringToIndex:2];
...has no target/receiver. You should simply move this line to the line before you dismiss the modalView so that the chosenCountry
object is still alive when the message is sent.
精彩评论