保留计数问题:请提供一些指导
[我确信这一点也不奇怪,但我只需要一点帮助]
我有两个 retain
属性
@property (nonatomic, retain) NSArray *listContent;
@property (nonatomic, retain) NSArray *filteredListContent;
,在 viewDidLoad
方法中我将第二个设置为相等到第一个(所以现在保留计数是二,我认为):
self.filteredListContent = self.listContent;
然后在每次执行此操作时,
self.filteredListContent = [listContent filteredArrayUsingPredicate:predicate];
我认为我应该在该分配的正上方执行一个 release
——因为该属性应该导致额外的保留吧? -- 但这会导致程序在我第二次运行搜索方法时爆炸。我第一次进入搜索方法时,保留计数(没有额外的 release
)为 2,随后每次都是 1(不幸的是,这正是我所期望的)。
一些指导会有所帮助,谢谢!不释放是正确的吗?
[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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你不必释放它,这是正确的。
因为变量存储在两个位置,所以它的保留计数应该是 2。这就是它崩溃的原因。 (在括号中保留 self.listContent 的计数。)
self.listContent
过早释放。如果您不使用[...release]
,则保留计数数学将起作用。阅读 Vincent Gable 的博客,了解有关何时使用的简短摘要释放。 (有趣的是,这篇博文的灵感来自Andiih 在 Stackoverflow 上的回答。)
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
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.)
不,您不需要在运行过滤器搜索之前进行
retain
调用。该财产的任何旧价值都将被释放。第一次检查保留计数时,
self.filteredListContent
和self.listContent
引用同一个数组对象,并且都具有该数组的引用计数器。搜索后,self.listContent
的保留计数降至 1,因为在设置搜索结果(并随后保留)时,self.filteredListContent
释放了它。No, you do not need to make a
retain
call prior to running the filter search. Any old value in the property will be released.The first time you check the retain count,
self.filteredListContent
andself.listContent
reference the same array object and both have a reference counter for that array. After the searchself.listContent
's retain count drops to 1 because it was released byself.filteredListContent
when the search results were set (and subsequently retained).如果您没有 NARC* 该对象,则无需
释放
它。*NARC - 新建、分配、保留、复制
保留属性既
保留
分配时的新值,又释放
发生分配时的旧值。If you didn't NARC* the object, you don't need to
release
it.*NARC -- New, Alloc, Retain, Copy
A retained property both
retain
s the new value on assignment andrelease
s the old value when that happens.