Objective C,内存管理

发布于 2024-09-28 03:18:28 字数 313 浏览 4 评论 0原文

1)使用retain的原因是什么?

例如,在setter方法中:

- (void) setCount: (int) input {
    [intCount autorelease];
    intCount = [input retain];
}

2)autorelease方法:是删除旧对象还是准备新对象?

3)为什么在输入对象处调用retain-方法?

intCount = input;

不会出错? 为什么?

1) What is the reason for the use of retain?

For example, in a setter method:

- (void) setCount: (int) input {
    [intCount autorelease];
    intCount = [input retain];
}

2) the autorelease-Method: Is it deleting an old object or preparing the new one?

3) Why the retain-method is called at the input-object?

Would

intCount = input;

be wrong?
And why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

只涨不跌 2024-10-05 03:18:28
  1. Retain 用于增加对象的retainCountNSObjects 有一个名为 retainCount 的属性,它维护当前对象上持有的引用数量的计数。当对象的retainCount达到0时,该对象就可以从内存中释放。这实际上可以防止对象在其他地方仍在使用时从内存中释放。

  2. autorelease 方法删除旧对象,并且准备新对象。它实际上是释放对象的抢占式调用(自动释放比这复杂得多,您应该在 内存管理指南。)

  3. 在您的情况下 intCount = input 不会错,因为您正在使用原语。但是,如果输入是一个对象,那么您需要对其调用保留。事实上,您甚至不需要为基元(或对象)编写自己的 getter/setter,而应该使用 声明的属性。事实上,使用声明的属性几乎总是更好,如果您确实想推出自己的属性,请了解 这样做的陷阱 首先。

  1. Retain is used to increment the retainCount of an object. NSObjects have a property called retainCount which maintains a count on the number of references that are currently held on an object. When the retainCount of an object reaches 0, the object can be released from memory. Effectively this prevents an object from being released from memory if it's still in use elsewhere.

  2. The autorelease method does not delete an old object and does not prepare the new object. It is effectively a pre-emptive call to release the object (autorelease is much more complicated than that and you should read up on it in the Memory Management Guide.)

  3. In your case intCount = input wouldn't be wrong because you're working with a primative. However if input were an object then you'd need to be calling retain on it. In fact you don't even need to be writing your own getters/setters for primatives (or objects), but instead should be using Declared Properties. In fact you're almost always better off using Declared Properties and if you do want to roll your own, get to know the pitfalls of doing so first.

病毒体 2024-10-05 03:18:28

您的问题的答案已经得到了相当好的回答,所以让我补充一下,如果您可以使用垃圾收集,那么您应该这样做。它让一切变得更加容易。它不是万能药,您仍然应该学习使用保留/释放机制,但除非您正在处理一些大容量内存调用、创建和删除大量对象,否则只需使用垃圾收集。

它可以在项目 | 下找到。编辑项目设置 |构建

然后只需搜索“垃圾”,你就会看到它。

如果您正在进行 iOS 开发并且无法使用垃圾收集,我很抱歉提供无用的信息,但它仍然代表非 iOS 开发。

The answers to your questions have been answered fairly well so let me just add that if you can use garbage collection you should. It makes everything so much easier. It isn't a panacea and you still should learn to use the retain/release mechanism but unless you are dealing in some high volume memory calls, creating and deleting lots of objects, then just use garbage collection.

It can be found under Project | Edit Project Settings | Build

Then just search for "garbage" and you'll see it.

If you are doing iOS development and cannot use garbage collection I apologize for giving unhelpful information but it still stands for non-iOS development.

屋顶上的小猫咪 2024-10-05 03:18:28

具体回答你的问题:

1)。 keep的用途是声明一个对象的所有权。在这种情况下,intCount保留input的所有权,万一input在其他地方被释放,你仍然可以使用intCount

2)。 intCount的自动释放是放弃旧值的所有权。这避免了旧值的内存泄漏。如果你不释放旧值,并且给这个指针分配一个新值,那么旧对象将永远存在并且永远不会被释放,这将导致内存泄漏。

3)。如果你不保留输入,并且输入的参数被释放到其他地方。那么如果没有其他地方保留这个对象,它将被释放。所以你也不能使用intCount。这就是为什么你需要保留它或复制它。

但我认为如果你这样做 intCount = input; 应该没问题。因为 int 不是对象,它只是一种类型。所以我认为整个方法可以这样写:

- (void) setCount: (int) input {
    intCount = input;
}

但是如果它是一个指针,你不应该直接将新值赋给旧值。

To answer ur question specifically:

1). the use of retain is to declare the ownership of an object. In this case, intCount retains the ownership of input, in case the input got released somewhere else, u can still use the intCount.

2). the autorelease of intCount is to relinquish the ownership of the old value. That avoid the memory leak of the old value. If u don't release the old value, and u assign a new value to this pointer, the old object will always be there and never got released, which will cause the memory leak.

3). if u don't retain the input, and the parameter of input got released somewhere else. then if nowhere else retain this object, it will get freed. So u can't use intCount as well. That's why u need to retain it or copy it.

But i think if u do intCount = input; it should be fine. Because int is not an object, it's just a type. So I think the whole method is okay to be written like this:

- (void) setCount: (int) input {
    intCount = input;
}

But if its a pointer, u should not assign the new value to the old one directly.

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