保留计数问题:请提供一些指导 [英] Retain Count Question: Some Guidance, Please

查看:50
本文介绍了保留计数问题:请提供一些指导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[我确定这一点都不奇怪,但是我只需要一点帮助]

[I'm sure this is not odd at all, but I need just a bit of help]

我有两个retain属性

@property (nonatomic, retain) NSArray *listContent;
@property (nonatomic, retain) NSArray *filteredListContent;

,然后在viewDidLoad方法中,将第二个设置为等于第一个(所以我认为现在的keepCount是两个):

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];

我认为我应该在此任务上方执行release,因为该属性会引起额外的保留,对吗? -但这会导致程序我第二次运行搜索方法时爆炸.保留计数(不带额外的release)是我第一次进入搜索方法时的计数,而每次以后都为1(不幸的是,这是我所期望的).

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.

由于该变量存储在两个位置,因此其保留计数应为2.这就是它崩溃的原因. (将self.listContent的计数保留在方括号中.)

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被释放太早.如果您不使用[... release],则保留计数数学功能会起作用.

self.listContent gets deallocated too early. If you don't use [... release]it the retain count math works.

阅读 Vincent Gable的博客,其中简要介绍了何时使用 release . (有趣的是,此博客文章的灵感来自

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.)

这篇关于保留计数问题:请提供一些指导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆