简体   繁体   中英

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.

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.

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.

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. Autorelease will decrease the retain count by 1 in its cycle. (still you have another 1 retain count to release the object)

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).

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

As you know, objective c objects are reference counted. Any objective c object will be released if and only if its reference count reaches zero, no exceptions.

When an object is added to an NSMutableArray, the array will retain the object. 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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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