Where to dealloc pickerview?
In a modal view I have a UIPickerView that is allocated and initialized in viewDidLoad
.
It is deallocated in dealloc
with the other objects used in this view.
Is this an incorrect way of deallocating my picker? Why would it need to be accessed after the modal view is dismissed as the error message seems to be suggesting?
The zombie log error is:
-[UIPickerView release]: message sent to deallocated instance 0x5a441
Relevant code:
开发者_开发问答- (void)viewDidLoad
{
[super viewDidLoad];
//navigation bar stuff here
mcPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 244, 320, 216)];
mcPickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
// setup the data source and delegate for this picker
mcPickerDataSource = [[MCPickerDataSource alloc] init];
mcPickerView.dataSource = mcPickerDataSource;
mcPickerView.delegate = mcPickerDataSource;
mcPickerView.showsSelectionIndicator = YES;
[self.view addSubview:mcPickerView];
}
- (void)dealloc
{
[mc dealloc];
[mcPickerView dealloc];
[mcPickerDataSource dealloc];
[mcVal release];
[super dealloc];
}
You should only call dealloc on super. Everything else should be release.
This answer will explain why: iPhone SDK: Dealloc vs. Release?
In your dealloc method, your [mcPickerView dealloc]
probably also dealloc'd mcPickerDataSource for you since mcPickerView holds references to mcPickerDataSource. Change your dealloc method to release everything apart from super should fix the problem.
anything alloc'd in viewDidLoad: should be released, not dealloc'd, in viewDidUnload. This is because viewDidLoad and viewDidUnload may be called several times over the life of the object. Dealloc is only called once in the object's lifetime.
精彩评论