什么自动保留和释放?
我有点理解什么会自动保留和释放,但我找不到任何关于此的官方数据,我认为我弄错了。
- 设置使用 @property(retain) 声明的属性将保留并释放旧值,但不会在没有
self 的情况下设置同一对象内的属性。
- 方法内的所有对象(包括参数)都会被保留创建/传递并在方法返回时释放。
- 方法返回的对象不会被释放,而是会被其返回的方法释放,因为它在方法内有局部变量。
我是不是做错了什么/忘记了什么?
当退货被丢弃时会发生什么?就像[object someMethodThatReturnsAnObject];
。它对于返回的方法来说不是本地的,所以我不确定它是否会被释放并且 xCode 会对此发出警告。
I kind of understood what retains and releases automatically but I can't find any official data on this and I think I got something wrong.
- Setting properties that are declared with @property(retain) will retain and release the old value, but not setting the properties inside the same object without
self.
- All objects inside a method(including parameters) are retained when created/passed and released when the method returns.
- An object that is returned by a method will not be released but instead will be released by the method its returned to since its local variable inside the method.
Did I get something wrong/forgot anything?
What will happen when the return is discarded? like [object someMethodThatReturnsAnObject];
. it will not be local to the method its returned to so I'm not sure if it will be released and xCode warns about it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要猜测,阅读文档。这是确定的。
有点像。不存在“无己设置属性”。也就是直接设置实例变量。
没有;请参阅文档。
一点也不;请参阅文档。
Instead of guessing, read the documentation. It is definitive.
Sort of. There is no "setting a property without self". That is setting an instance variable directly.
Nope; see the docs.
Not at all; see the docs.
调用为保留属性合成的 setter 方法将释放旧值并保留新值。直接设置 ivar,而不通过访问器,除了分配 ivar 之外不会执行任何操作。它既不会保留也不会释放任何东西。
self.foo = bar
完全是[self setFoo:bar]
。无论setFoo:
做什么,都会完成(setFoo:
的合成保留版本的工作原理如上所述)。foo = bar
完全是foo = bar
。这根本不是真的。它们既不保留也不释放。保留和释放不会神奇地发生。一方面,它是响应对
alloc
、new
、copy
、retain
和的调用而发生的。在另一个上释放
。使用点表示法只是方法调用的简写,其中可能有retain
。这不是真的。方法返回的对象既不会被保留也不会被释放。按照惯例,名称中包含
alloc
、new
或copy
的方法将返回 net +1 保留。任何其他方法都将返回净 0 保留计数(对象上的自动释放次数与保留次数一样多)。 “当前”保留计数将始终大于 0,否则无法返回对象。 的思考方式。)(这是对事实的轻微粉饰。如果存在私有保留,则任何一种情况下的保留计数都可能大于 1。但从调用者的角度来看,这是一种有用 值得一看的地方是实用内存管理,非常简洁地阐述了这一切。内存管理编程指南的其余部分将给出更多示例。
Calling a setter method that was synthesized for a retain property will release the old value and retain the new one. Setting an ivar directly, without going through your accessor, will do nothing but assign the ivar. It will neither retain nor release anything.
self.foo = bar
is exactly[self setFoo:bar]
. WhateversetFoo:
does will be done (a synthesized retain version ofsetFoo:
works as described above).foo = bar
is exactlyfoo = bar
.This is not true at all. They are neither retained nor released. Retaining and releasing does not happen magically. It happens in response to calls to
alloc
,new
,copy
,retain
on the one hand andrelease
on the other. Using dot notation is just a short-hand for a method call which may have aretain
inside of it.This is not true. An object returned by a method will be neither retained nor released. By convention, a method with
alloc
,new
orcopy
in its name will return a net +1 retain. Any other method will return a net 0 retain-count (there will be as many autoreleases on the object as retains). The "current" retain count will always be greater than 0 or else the object could not be returned. (This is a slight gloss on the truth. The retain count in either case could be greater than 1 if there are private retains. But from the point of view of the caller, this is a useful way to think about it.)The best place to look is Practical Memory Management, which lays this all out very concisely. The rest of the Memory Management Programming Guide will give more examples.
“自动”这个词有点可怕。这意味着保留和释放是由运行时在幕后以某种方式神奇地应用的。确实如此,唯一一次保留某些内容是在发送包含以下内容之一的消息时:New Alloc< /em> 保留 复制 (NARC)。它被释放的唯一时间是发送释放消息或自动释放消息时。那么你真正需要了解的是,这些方法是在什么情况下发送的?
1.设置属性
就你而言,你是对的。为什么?因为属性只是生成 getter 和 setter 方法的语法糖。例如,
self.myString = @"Foo";
与[self setMyString:@"Foo"]
完全相同。您需要了解的是,当您使用保留语义声明属性时,您实际上会获得一个如下所示的方法:因此,显然,说
self.myString = someOtherString
将导致新的所要保留的价值。您所说的“在没有self.
的情况下设置属性”实际上只是直接 ivar 赋值。由于没有使用点运算符,因此没有调用任何方法,因此您知道没有保留任何内容。2. 3. 与方法范围有关?
这些都不是真的。惯例规定,返回我的方法且名称中没有 NARC 痕迹的变量将被自动释放。由方法作者实际遵循约定。没有任何东西会因为方法调用而自动保留或释放。
The term "Automatically" is a bit scary. It implies that the retain and release are somehow magically applied by the runtime behind the scenes. Really truly, the only time something is retained is when it is sent a message that contain one of the following: New Alloc Retain Copy (N.A.R.C). The only time it is released is when it is sent either an release message or an autorelease message. So what you really need to understand is, in what situations are these methods sent?
1. Setting Properties
You're correct, in as far as you went. Why? Because properties are just syntactic sugar for generating getter and setter methods. For example,
self.myString = @"Foo";
is absolutely identical to[self setMyString:@"Foo"]
. What you need to understand is that, when you declare a property with retain semantics, you're actually given a method that looks like this:Thus, obviously, saying
self.myString = someOtherString
will cause the new value to be retained. What you refer to as "Setting a property withoutself.
" is actually just direct ivar assignment. Since no dot operator is used, and therefor no method invoked, you know that nothing is retained.2. and 3. Something To Do With Method Scope?
Neither of these are true at all. Convention says that variables returned my methods without traces of NARC in the name will be autoreleased. It's up the method author to actually follow the convention. Nothing is ever automatically retained or released just as a result of method invocation.