简体   繁体   English

iPhone-有关保留数组的问题

[英]iphone - a question about retaining an array

I am sorta new on Objective-C and I am trying to understand some stuff about this retain release mechanism. 我对Objective-C有点陌生,我试图了解有关此保留释放机制的一些知识。

Suppose I need an array to last the entire life of the application. 假设我需要一个数组来维持应用程序的整个生命周期。 So, suppose I create the array using something like 因此,假设我使用类似的方法创建数组

myArray = [[NSMutableArray alloc] init];

at the beginning of the app. 在应用程序的开头。

During the app, this array may have all objects removed, added objects from other arrays, etc. Suppose also, that during one of these operations of adding objects, I add autoreleased objects to the array. 在应用程序期间,此数组可能会删除所有对象,并从其他数组中添加对象,等等。另外,假设在添加对象的这些操作之一中,我向数组添加了自动释放的对象。 Two questions: 两个问题:

  1. will the objects added to that array always be live and never deallocated while the array is allocated? 分配数组时,添加到该数组的对象将始终处于活动状态并且永不释放吗?

  2. I know that adding an object to an array will increase its retain count. 我知道将对象添加到数组将增加其保留计数。 Is this also valid for autoreleased objects? 这对自动释放的对象也有效吗? (perhaps autoreleased arrays coming from other methods) (也许自动释放的数组来自其他方法)

thanks 谢谢

The answer to your first question is yes - any collection object by definition will keep its collected objects live until the collection itself is deallocated. 第一个问题的答案是肯定的 -根据定义,任何集合对象都将保持其收集的对象处于活动状态,直到集合本身被释放为止。

As for your second question, well, an object which is sent the autorelease message will have ownership transferred over to the current autorelease pool until that object is owned by another scope at which point the object is removed from the pool. 对于第二个问题,好吧,发送autorelease消息的对象将拥有权转移到当前的自动释放池中,直到该对象被另一个作用域拥有为止,此时该对象将从池中删除。

For example: 例如:

[[[NSArray alloc] init] autorelease];

is not a memory leak (and you do not own the object) assuming there is an autorelease pool available. 不是内存泄漏(和你没有自己的对象)假设有一个可用的自动释放池。

The fundamental is object is released when retain count is reached 0. 基本是在保留计数达到0时释放对象。

So now answering your question : The object in the array have the owner ship on its objects. 因此,现在回答您的问题:数组中的对象拥有所有者。 So until array is released the objects in the array collection will be retained. 因此,在释放数组之前,将保留数组集合中的对象。

Regarding the autorelease objects --- autorelease only talk about the present ownership, so when you add a autoreleased object to array collection then array increase the retain count by 1 and own the ownership. 关于自动释放对象---自动释放仅讨论当前所有权,因此,当您将自动释放对象添加到数组集合时,数组将保留计数增加1并拥有所有权。 Autorelease will decrease the retain count by 1 in its cycle. 自动释放会在其周期中将保留计数减少1。 (still you have another 1 retain count to release the object) (仍然还有1个保留计数来释放对象)

Note: Autorelease -- adding object to Autorelease pool will make a retain count to be decremented in its drain cycle --- object will only be deallocated when retain count is 0 (there there is no owners). 注意:自动释放-将对象添加到自动释放池将使保留计数在其消耗周期中减少---仅当保留计数为0(没有所有者)时,对象才会被释放。

我更喜欢在结束时释放它,而不是自动释放。.控制何时释放它似乎是一个更好的选择。.当dealloc只是:[myArray removeAllObjects],然后是[myArray release]时,函数结束。

As you know, objective c objects are reference counted. 如您所知,目标c对象是引用计数。 Any objective c object will be released if and only if its reference count reaches zero, no exceptions. 当且仅当其引用计数达到零且没有例外时,任何目标c对象才会被释放。

When an object is added to an NSMutableArray, the array will retain the object. 将对象添加到NSMutableArray时,数组将保留该对象。 That means, as long as you've not retained/released the object elsewhere, the object added to the array will be alive until (1) the object is removed from the array, and (2) the array is deallocated itself, where it will release every object stored in it. 这意味着,只要您没有在其他地方保留/释放对象,添加到数组的对象就会一直存在,直到(1)从数组中删除该对象,以及(2)数组本身被释放后,在该位置将释放存储在其中的每个对象。

Sending "autorelease" to an object adds it to an enclosing autorelease pool. 将“自动释放”发送到对象会将其添加到封闭的自动释放池中。 That means the object will be sent a release message when the autorelease pool is drained / deallocated. 这意味着当自动释放池被耗尽/释放时,将向对象发送释放消息。 Whether the object will be deallocated depends on whether some other code has retained it. 对象是否将被释放取决于其他一些代码是否保留了它。

will the objects added to that array always be live and never deallocated while the array is allocated? 分配数组时,添加到该数组的对象将始终处于活动状态并且永不释放吗?

They will under normal circumstances, ie, provided you're not overreleasing them . 在正常情况下,即在您不过度释放它们的情况下,它们会。 For instance, 例如,

// take ownership (alloc) followed by relinquish ownership (autorelease)
// the net result is that this code snippet DOES NOT own someObject
SomeClass *someObject = [[[SomeClass alloc] init] autorelease];

// myArray takes ownership of someObject
[myArray addObject:someObject];

// someObject is INCORRECTLY (over)released
[someObject release];

Considering that no other code has claimed ownership of someObject , it won't be 'live' because it was overreleased. 考虑到没有其他代码声明对someObject所有权,因此不会被“释放”,因为它已被过分释放。

I know that adding an object to an array will increase its retain count. 我知道将对象添加到数组将增加其保留计数。 Is this also valid for autoreleased objects? 这对自动释放的对象也有效吗? (perhaps autoreleased arrays coming from other methods) (也许自动释放的数组来自其他方法)

Yes. 是。 Collections won't look at retain counts or autorelease status whilst adding objects, nor they should. 集合在添加对象时不会查看保留计数或自动释放状态,也不会。 An array simply sends -retain to the object being added and hence takes ownership of that object, regardless of other code owning (or not owning) the object. 数组只是将-retain发送到要添加的对象,因此获得该对象的所有权,而与拥有(或不拥有)该对象的其他代码无关。

The whole point of memory management and object ownership is to consider ownership in a relative way: if a collection needs an object, it'll take ownership of the object; 内存管理和对象所有权的全部要点是相对考虑所有权:如果集合需要一个对象,它将获取该对象的所有权; if the collection is released or the object is removed from the collection, it'll relinquish ownership of the object. 如果发布了集合或从集合中删除了对象,它将放弃对象的所有权。 The collection doesn't care about the object being owned by other objects or code — it is only concerned about its own perspective of owning the object. 该集合并不关心该对象是否由其他对象或代码拥有,而是仅关注其拥有该对象的角度。 The same principle should apply to your code. 同样的原则也适用于您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM