简体   繁体   English

obj-c 自动释放一个变量到一个方法链上

[英]Obj-c autorelease a variable up a chain of methods

I'm new to Obj-C and I have a question concerning the autorelease.我是 Obj-C 的新手,我有一个关于自动发布的问题。 Is it ok to return an autoreleased variable for several methods?可以为几种方法返回一个自动释放的变量吗? For example:例如:

- (void) methodC {
    Object anObj = [self methodB];
    //Do something with anObj
}

- (Object *) methodB {
    return [self methodA];
}   

- (Object *) methodA {
    Object anObj = [[anObj alloc] init];
    release [anObj autorelease];  
}

Will the variable remain valid even if it is returned up a method chain and used at the top?即使变量返回到方法链并在顶部使用,它是否仍然有效? Or does it have to be retained somewhere along the way?还是必须沿途保留它?

thank you谢谢你

Yes, it will be valid in this case.是的,在这种情况下它是有效的。 You only have to worry about the variable being deallocated if somebody drains the autorelease pool.如果有人耗尽了自动释放池,你只需要担心变量被释放。 As long as you've written every function that returns along the way and you don't explicitly drain the autorelease pool, you don't have to worry about objects being deallocated from under you.只要您编写了沿途返回的每个 function 并且您没有显式地耗尽自动释放池,您就不必担心从您下面释放对象。

In the vast majority of cases, the code in the NSRunLoop takes care of draining the autorelease pool.在绝大多数情况下, NSRunLoop中的代码负责清空自动释放池。 When you return control from your application code to the API code (such as by returning from a touchesBegan handler etc.), you don't know if the autorelease pool will be drained, so you have to assume in the worst case that it will.当您从应用程序代码将控制权返回给 API 代码时(例如通过从touchesBegan处理程序返回等),您不知道自动释放池是否会耗尽,因此您必须假设在最坏的情况下它会. In that case, you have to retain any objects you want to keep references to.在这种情况下,您必须retain要保留引用的任何对象。

For example:例如:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

Object *anObj = [self methodC];  // Your methodC as before -- anObj is valid

[pool drain];  // anObj will be deallocated here

The variable should remain valid.该变量应保持有效。 You only need to retain an object if it is actually "owned" by some other object and could be indirectly/unintentionally released along with it.您只需要保留 object 如果它实际上由其他一些 object “拥有”并且可能会间接/无意地与它一起发布。 For example, if you extracted an object from an array and then released the array, your object reference could become invalid unless you explicitly retain it.例如,如果您从数组中提取 object 然后释放该数组,则您的 object 引用可能会变为无效,除非您明确保留它。

For more details, see Object Ownership and Dismissal , particularly the sections on Autorelease and Validity of Shared Objects.有关更多详细信息,请参阅Object Ownership and Dismissal ,尤其是共享对象的自动释放和有效性部分。 The latter uses the following code to illustrate how you could "accidentally" make an object reference invalid.后者使用以下代码来说明如何“意外”使 object 引用无效。

heisenObject = [array objectAtIndex:n];

[array removeObjectAtIndex:n];

// heisenObject could now be invalid.

The following code shows how to mitigate this problem using retain .下面的代码展示了如何使用retain来缓解这个问题。

heisenObject = [[array objectAtIndex:n] retain];

[array removeObjectAtIndex:n];

// use heisenObject.

[heisenObject release];

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

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