通过分配保留 self 对象是个好主意吗
我正在做一些分配,因为
self.xyz = [[NSDictionary alloc] init];
保留这样的财产是个好主意吗?还是
这样做会更好,例如:
NSDictionary *zzz = [[NSDictionary alloc] init];
self.xyz = zzz;
[zzz release];
我在这里关心的是,我看到一些地方有人保留,例如:
self.xyz = [[NSDictionary alloc] init];
这意味着保留计数是 2。那么将这里的计数减少到 1 的最佳方法是什么。
谢谢。只是想更清楚地了解一些内存管理概念。
I am doing some allocation as
self.xyz = [[NSDictionary alloc] init];
Is it a good idea of retaining a property such like that? Or
will it be better to do such as:
NSDictionary *zzz = [[NSDictionary alloc] init];
self.xyz = zzz;
[zzz release];
My concern here is, I have seen some places people retaining such as:
self.xyz = [[NSDictionary alloc] init];
which means the retain count is 2. So what's the best way to reduce a count here to one.
Thanks. Just trying to clear some memory management concept clear a little more.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这取决于您处理属性
xyz
的方式。如果您这样做,则
xyz
在NSDictionary
上有一个保留,因此第二个(3 行)版本是最好的。This depends on how you've handled the property
xyz
. If you didthen
xyz
has a retain on theNSDictionary
, so the second (3 line) version is best.我通常会这样做:
或者,如果该类有一个方便的方法,则像这样使用它:
或者,只使用 ARC 并让它在这种情况下为您完成工作。
I usually would do this:
Or if the class has a convenience method then use it like so:
Or, just use ARC and let it do the work for you in this instance.
我假设我们正在讨论不使用 ARC 时的最佳实践。
在手动内存管理环境中,第一种方法是完全错误的,因为正如您所指出的,保留计数为 2。执行单行的正确方法如下所示:
遍历综合设置器(假设 xyz 是用保留指令声明的),除了
alloc
/init
添加 1 之外,还向保留计数添加 1。自动释放可以平衡这个问题。您详细介绍的第二种方法在功能上是等效的,但由于可用内存相对较少,因此被认为是嵌入式设备上的更好实践。您创建一个对象,将其分配给属性并立即释放原始临时对象。在前一种方法中,对象被放入自动释放池中并在稍后的时间点释放。
I'm assuming we're talking about best practices when NOT using ARC.
In a manual memory management environment, the first approach is flat out wrong because as you pointed out, the retain count is at 2. The proper way to do a one liner is like so:
Going through the synthesized setter (assuming
xyz
is declared with the retain directive), adds 1 to the retain count in addition to the 1 being added byalloc
/init
. The autorelease is there to balance this out.The second approach that you detailed is functionally equivalent, but considered a better practice on embedded devices because of the relatively small amounts of memory available. You create an object, assign it to the property and release the original temporary object immediately. In the former approach, the object is placed into an autorelease pool and released at a later point in time.
我认为您应该使用自动释放作为代码的最佳实践。
I think you should use autorelease as a best practices for your code.
第二种方法最好将保留计数保持为 1。
The second approach is the best to maintain retain count as 1.
您必须始终平衡内存管理等式,否则可能会出现内存泄漏。
在您的示例中,它取决于 xyz 内存管理策略。
如果 xyz 有保留策略,则保留计数为 2。这是一个常见的错误,并且存在内存泄漏。
如果 xyz 有分配策略,则不会增加保留计数
总之,如果您使用保留策略,则您提供的第二个片段是正确的方法。显然你必须记住在 dealloc 方法中释放该属性。
希望有帮助。
You have always to balance memory-management equation, if not, you could have memory leaks.
In your example it depends on xyz memory management policy.
If xyz has a retain policy, you have a retain count of two. It's a common mistake and you have a memory leak.
If xyz has an assign policy, you don't increment the retain count
In conclusion, if you use a retain policy, the second snippet you provided is the right way. Obviously you have to remember to release that property in dealloc method.
Hope it helps.
依我愚见,
会造成内存泄漏。需要手动释放。
你可以这样做
之后,即使我也有同样的问题。哪种方法更好?为什么?
In my humble opinion,
will cause memory leak. You need to release manually.
You can do this
After that even I have the same question. Which approach is better? and why?