OK, so I've got my pet project working - now I'm fine tuning and trying to see if I have any leaks. I was ridiculously anal retentive with any piece of memory I took ownership of. It was only until I hooked up my model to my UI where I thought of a potential pitfall.
To give some background, I have a UISearchBar and a UITableView in my main UIView. In my UITableViewDelegate's searchBarTextDidBeginEditing method, I cache the SearchBar's current text into a global variable called previousText and then clear the searchBar's text:
I make a copy of it because as soon as I set the text equal to a blank string, the original reference in the text property is released (hence it's no good to me). The purpose for caching is in case the user clicks the Cancel button, I'd like the value to be reverted to what was there before editing began.
Then in my UITableViewDelegate's searchBarTextDidEndEditing, I perform my search if the cancel button wasn't clicked, reset the text back from the cache if the cancel button was clicked, and finally release previousText:
So, I've covered my bases such that my cached text will be around for the duration of editing. But what happens if the user clicks the Home button during editing? I'm not too knowledgeable about what happens at that point. I figure the application delegate is sent a dealloc call which sets off a chain of dealloc calls to all child objects. Now currently, I'm not calling release on my global variable in my UITableViewDelegate's dealloc method. I just thought of this at the time of writing this. Calling release on a pointer that has already been released and won't be used again is fairly safe, right?
If I didn't put this safegaurd release call in my UITableViewDelegate's dealloc method, would the release of the application's main AutoReleasePool upon termination clean that up if the user clicked the Home button in between editing. What would be the easiest way to test this?
Thanks for any input you guys can provide!
To give some background, I have a UISearchBar and a UITableView in my main UIView. In my UITableViewDelegate's searchBarTextDidBeginEditing method, I cache the SearchBar's current text into a global variable called previousText and then clear the searchBar's text:
Code:
previousText = [searchBar.text copyWithZone:nil];
searchBar.text = @"";
I make a copy of it because as soon as I set the text equal to a blank string, the original reference in the text property is released (hence it's no good to me). The purpose for caching is in case the user clicks the Cancel button, I'd like the value to be reverted to what was there before editing began.
Then in my UITableViewDelegate's searchBarTextDidEndEditing, I perform my search if the cancel button wasn't clicked, reset the text back from the cache if the cancel button was clicked, and finally release previousText:
Code:
if (!cancelButtonClicked) {
[dataSource update:searchBar.text];
[tableView reloadData];
}
else {
searchBar.text = previousText;
}
[previousText release];
So, I've covered my bases such that my cached text will be around for the duration of editing. But what happens if the user clicks the Home button during editing? I'm not too knowledgeable about what happens at that point. I figure the application delegate is sent a dealloc call which sets off a chain of dealloc calls to all child objects. Now currently, I'm not calling release on my global variable in my UITableViewDelegate's dealloc method. I just thought of this at the time of writing this. Calling release on a pointer that has already been released and won't be used again is fairly safe, right?
If I didn't put this safegaurd release call in my UITableViewDelegate's dealloc method, would the release of the application's main AutoReleasePool upon termination clean that up if the user clicked the Home button in between editing. What would be the easiest way to test this?
Thanks for any input you guys can provide!