Should a (copy) @property be released? Is Apple's sample code correct?
This question is refers to Apple's example "TableSearch" project that implements a searchable table view. The relevant source code can be found here:
MainViewController.h
MainViewController.m
In this example project, the "MainViewController" class has a property to save the search term:
@property (nonatomic, copy) NSString *savedSearchTerm;
But the dealloc does not release "savedSearchTerm":
- (void)dealloc
{
[listContent release];
[filteredListContent release];
[super dealloc];
}
(The example code does开发者_JAVA技巧n't release the "savedSearchTerm" anywhere else (although it does set it to nil under some circumstances in viewDidLoad)).
Given that Apple's memory management rules say that you should release objects created using “alloc”, “new” or “copy”, why doesn't "savedSearchTerm" need to be released?
It should be released in the -dealloc
method. If the sample code is not doing that, please file a bug at http://bugreport.apple.com and it will be fixed.
I haven't seen the sample, but as you describe it, saveSearchTerm needs to be released.
either via
self.saveedSearchTerm = nil;
or
[savedSearchTerm release];
if you see simply
savedSearchTerm = nil;
then this is likely a memory leak as it is just setting the ivar to nil without releasing the object.
精彩评论