保留计数问题:请提供一些指导

发布于 2024-09-02 18:14:32 字数 687 浏览 6 评论 0原文

[我确信这一点也不奇怪,但我只需要一点帮助]

我有两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

诗酒趁年少 2024-09-09 18:14:32

你不必释放它,这是正确的。

因为变量存储在两个位置,所以它的保留计数应该是 2。这就是它崩溃的原因。 (在括号中保留 self.listContent 的计数。)

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],则保留计数数学将起作用。

阅读 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 = 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.)

冰魂雪魄 2024-09-09 18:14:32

不,您不需要在运行过滤器搜索之前进行 retain 调用。该财产的任何旧价值都将被释放。

第一次检查保留计数时,self.filteredListContentself.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 and self.listContent reference the same array object and both have a reference counter for that array. After the search self.listContent's retain count drops to 1 because it was released by self.filteredListContent when the search results were set (and subsequently retained).

帅冕 2024-09-09 18:14:32

如果您没有 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 retains the new value on assignment and releases the old value when that happens.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文