2010-05-23 43 views
9

我正在研究一个iPhone项目,我想从NSMutableArray中检索一个对象,从该数组中删除该对象,然后再使用它。代码如下所示:是否NSArray:lastObject返回一个自动释放对象?

NSMutableArray * array; 
// fill the array 
NSObject * obj = [array lastObject]; 
[array removeLastObject]; 
// do something with obj (in the same function) 

数组是唯一的实体在被访问的对象上有一个保留。这是安全的吗?我会假设这只会在lastObject自动释放对象的时候起作用,这是我无法弄清楚的。

回答

11

为什么不叫

[array removeLastObject] 

在你的函数结束了吗?这是少一个版本/保留。它可能会使您的代码更易读/更少混乱。

备案Apple documentation

Like NSArray, instances of NSMutableArray maintain strong references to their contents. If you do not use garbage collection, when you add an object to an array, the object receives a retain message. When an object is removed from a mutable array, it receives a release message. If there are no further references to the object, this means that the object is deallocated. If your program keeps a reference to such an object, the reference will become invalid unless you send the object a retain message before it’s removed from the array. For example, if anObject is not retained before it is removed from the array, the third statement below could result in a runtime error:

id anObject = [[anArray objectAtIndex:0] retain]; 
[anArray removeObjectAtIndex:0]; 
[anObject someMessage]; 
+0

为了使结果清楚:NSArray和NSMutableArray类有一些例外,这些通用规则是从名称不包含“new”,“copy”或“alloc”返回自动释放对象的方法返回的对象,这意味着代码在原来的问题是不安全的 – harms 2010-05-24 00:19:18

+0

@harms不是这个规则只适用于对象创建? – sylvanaar 2010-05-24 00:35:05

+4

@harms:没有通用规则,从名称不包含“new”,“copy”或“alloc”的方法返回的对象返回自动释放对象。唯一的规则是您不拥有这样的对象。 – JeremyP 2010-05-24 09:25:06

2
NSObject * obj = [array lastObject]; // Just returns an object, its retain count not changed 
[array removeLastObject]; // object receives release message 

所以,如果你的阵列是保留您的对象,然后取出它的保留计数后的唯一实体变为零,它应该得到释放。要使用该对象,你应该保留它,并释放不需要的时候安全地工作:

NSObject * obj = [[array lastObject] retain]; 
[array removeLastObject]; 
// do something with obj (in the same function) 
[obj release]; 
3

值得一提这里的Cocoa Memory Management Rules。如果您在非GC环境中工作,例如iPhone,它总是值得一提的。

基本规则状态:

You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message. You are responsible for relinquishing ownership of objects you own using release or autorelease. Any other time you receive an object, you must not release it.

显然没有用ALLOC,新...什么的含副本获得的obj。所以你不拥有它。

在你的代码中,推论的第二个也是相关的:

A received object is normally guaranteed to remain valid within the method it was received in (exceptions include multithreaded applications and some Distributed Objects situations, although you must also take care if you modify an object from which you received another object). That method may also safely return the object to its invoker

我已经加粗的相关部分。您已经修改了从中接收对象的数组,因此它可能会消失。您可以在从阵列中删除对象之前保留该对象,或者在完成对象后执行删除操作。

其实,你可能安全,即使在你的问题的代码,因为NSMutableArray中的实现的objectAtIndex的:可能做挽留随后在退货项目自动释放。在多线程环境中,这将是唯一的方法来确保对象保持足够长的时间以将其返回给调用者。但是,我没有看到NSMutableArray的代码,所以不要依赖它。

ETA:刚刚在看线程编程指南,看来NSMutableAtrray不会保留,autorelease。

+0

感谢您的回答,最后一行非常有用 – Chris 2011-05-10 05:59:36

相关问题