什么自动保留和释放?

发布于 2024-11-15 20:02:20 字数 383 浏览 3 评论 0原文

我有点理解什么会自动保留和释放,但我找不到任何关于此的官方数据,我认为我弄错了。

  1. 设置使用 @property(retain) 声明的属性将保留并释放旧值,但不会在没有 self 的情况下设置同一对象内的属性。
  2. 方法内的所有对象(包括参数)都会被保留创建/传递并在方法返回时释放。
  3. 方法返回的对象不会被释放,而是会被其返回的方法释放,因为它在方法内有局部变量。

我是不是做错了什么/忘记了什么?
当退货被丢弃时会发生什么?就像[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.

  1. 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.
  2. All objects inside a method(including parameters) are retained when created/passed and released when the method returns.
  3. 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 技术交流群。

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

发布评论

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

评论(3

感情废物 2024-11-22 20:02:20

不要猜测,阅读文档。这是确定的。

设置声明的属性
使用 @property(retain) 将保留并
释放旧值,但不设置
同一对象内的属性
没有自我。

有点像。不存在“无己设置属性”。也就是直接设置实例变量。

方法内的所有对象(包括
参数)被保留时
创建/传递并释放时
方法返回。

没有;请参阅文档。

方法返回的对象
不会被释放,但会
由其返回的方法释放
因为它的局部变量在
方法。

一点也不;请参阅文档。

Instead of guessing, read the documentation. It is definitive.

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.

Sort of. There is no "setting a property without self". That is setting an instance variable directly.

All objects inside a method(including
parameters) are retained when
created/passed and released when the
method returns.

Nope; see the docs.

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.

Not at all; see the docs.

煞人兵器 2024-11-22 20:02:20

设置使用 @property(retain) 声明的属性将保留并释放旧值,但不会在没有 self 的情况下设置同一对象内的属性。

调用为保留属性合成的 setter 方法将释放旧值并保留新值。直接设置 ivar,而不通过访问器,除了分配 ivar 之外不会执行任何操作。它既不会保留也不会释放任何东西。 self.foo = bar 完全[self setFoo:bar]。无论 setFoo: 做什么,都会完成(setFoo: 的合成保留版本的工作原理如上所述)。 foo = bar 完全foo = bar

方法内的所有对象(包括参数)在创建/传递时都会保留,在方法返回时会被释放。

这根本不是真的。它们既不保留也不释放。保留和释放不会神奇地发生。一方面,它是响应对 allocnewcopyretain的调用而发生的。在另一个上释放。使用点表示法只是方法调用的简写,其中可能有 retain

方法返回的对象不会被释放,而是会被其返回的方法释放,因为该对象在方法内有局部变量。

这不是真的。方法返回的对象既不会被保留也不会被释放。按照惯例,名称中包含 allocnewcopy 的方法将返回 net +1 保留。任何其他方法都将返回净 0 保留计数(对象上的自动释放次数与保留次数一样多)。 “当前”保留计数将始终大于 0,否则无法返回对象。 的思考方式。)

(这是对事实的轻微粉饰。如果存在私有保留,则任何一种情况下的保留计数都可能大于 1。但从调用者的角度来看,这是一种有用 值得一看的地方是实用内存管理,非常简洁地阐述了这一切。内存管理编程指南的其余部分将给出更多示例。

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.

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]. Whatever setFoo: does will be done (a synthesized retain version of setFoo: works as described above). foo = bar is exactly foo = bar.

All objects inside a method(including parameters) are retained when created/passed and released when the method returns.

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 and release on the other. Using dot notation is just a short-hand for a method call which may have a retain inside of it.

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.

This is not true. An object returned by a method will be neither retained nor released. By convention, a method with alloc, new or copy 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.

痕至 2024-11-22 20:02:20

“自动”这个词有点可怕。这意味着保留和释放是由运行时在幕后以某种方式神奇地应用的。确实如此,唯一一次保留某些内容是在发送包含以下内容之一的消息时:New Alloc< /em> 保留 复制 (NARC)。它被释放的唯一时间是发送释放消息或自动释放消息时。那么你真正需要了解的是,这些方法是在什么情况下发送的?

1.设置属性
就你而言,你是对的。为什么?因为属性只是生成 getter 和 setter 方法的语法糖。例如,self.myString = @"Foo";[self setMyString:@"Foo"] 完全相同。您需要了解的是,当您使用保留语义声明属性时,您实际上会获得一个如下所示的方法:

- (void)setMyString:(NSString *)newString {
    if ( newString != myString ) {
        [myString release];
        myString = [newString retain];
    }
}

因此,显然,说 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:

- (void)setMyString:(NSString *)newString {
    if ( newString != myString ) {
        [myString release];
        myString = [newString retain];
    }
}

Thus, obviously, saying self.myString = someOtherString will cause the new value to be retained. What you refer to as "Setting a property without self." 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.

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