PDA

View Full Version : What happens to allocated memory if Home Button is pressed




Enuratique
Jun 25, 2008, 04:07 PM
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:

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:

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!



Catfish_Man
Jun 25, 2008, 05:18 PM
Over-releasing things is definitely not safe. Personally I'd probably make the cached value an instance variable of the delegate, but it's hard to say for sure without looking it over more.

Enuratique
Jun 25, 2008, 05:32 PM
Over-releasing things is definitely not safe. Personally I'd probably make the cached value an instance variable of the delegate, but it's hard to say for sure without looking it over more.

Well yes, over releasing things is dangerous if your code can potentially use that reference later on in the code... In my case, the "safe gaurd release" would be in the dealloc - and hence it can be assumed it won't be needed anymore. What I meant by "safe" to call was that calling release on an already released reference or nil won't cause an exception to be thrown. It's impossible to do anything in my program other than tap away on the keyboard while in editing mode, so in that case, I'm not using that pointer until exiting editing mode.

Also, instance variables wouldn't help too much since I need to perform an operation in two different delegate methods.

Thanks for the reply, though :)