[I'm sure this is not odd at all, but I need just a bit of help]
I have two retain
properties
@property (nonatomic, retain) NSArray *listContent;
@property (nonatomic, retain) NSArray *filteredListContent;
and in the viewDidLoad
method I set the second equal to the first (so now the retainCount is two, I think):
self.filteredListContent = self.listContent;
and then on every search I do this
self.filteredListContent = [listContent filteredArrayUsingPredicate:predicate];
I thought I should do a release
right above this assignment -- since the property should cause an extra retain, right? -- but that causes the program to explode the second time I run the search method. The retain counts (without the extra release
) are 2 the first time I come into the search method, and 1 each subsequent time (which is what I expected, unfortunately).
Some guidance would help, thanks! Is it correct to not release?
You don't have to release it, that's correct.
Because the variable is stored in two locations, its retain count should be 2. Here's the reason it crashes. (Retain count of self.listContent in brackets.)
self.listContent = someArray [1] self.filteredListContent = self.listContent [2] [self.filteredListContent release] [1] self.filteredListContent = somethingElse [0] -> deallocation of listContent [self.listContent doSomething] [whoops, bad things happen]
self.listContent
gets deallocated too early. If you don't use [... release]
it the retain count math works.
Read Vincent Gable's blog for a really short summary on when to use release. (Interestingly, this blog post was inspired by Andiih's answer on Stackoverflow.)