0

可以说我有以下情况:

- (void)someFunction:(id)param {

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

    NSObject *objectA = [[NSObject alloc] init];
    [objectA doStuff];  // Don't need to release objectA because of the pool

    if (!someValue) {
        [pool release];   // Doubt here
        return;
    }

    NSObject *objectB = [[NSObject alloc] init];
    [objectB doStuff];  // Don't need to release objectB because of the pool

    [pool release];
}

以这种方式从泳池街区内返回是否正确?

4

1 回答 1

2

简短的回答:是的。

长答案:当释放它嵌套的自动释放池(通常是当前线程的顶级自动释放池)时,释放没有显式释放的池。

顺便说一句,这是错误的:

NSObject *objectA = [[NSObject alloc] init];
[objectA doStuff];  // Don't need to release objectA because of the pool

您仍然必须明确释放分配的对象:

NSObject *objectA = [[NSObject alloc] init];
[objectA doStuff];
[objectA release];

或将它们添加到自动释放池中:


NSObject *objectA = [[[NSObject alloc] init] autorelease];
[objectA doStuff];
于 2011-02-22T20:53:54.557 回答