2014-01-17 32 views
4

我有下面的代码片段:在哪里释放__block变量?

-(void) doSomething 
{ 
    __block NSMutableArray *objArray = [[NSMutableArray alloc] initWithCapacity:0]; 
     [self performOperationWithBlock:^(void) 
     { 
     //adding objects to objArray 
     . 
     . 
     //operation with objArray finished 

     // 1. should objArray be released here? 
     }]; 

     //2. should objArray be released here? 
} 

我应该自动释放的objArray?

+0

'__block'表示您不希望块保留一个rray,所以版本2更合乎逻辑。我会在施工时使用'autorelease'。 –

+2

如果您只是想将对象添加到块中的数组中,则不需要'__block'。 –

回答

5

如果它是一个异步调用,这将是有意义的创建实际块内的NSMutableArray

[self performOperationWithBlock:^(void) 
    { 
    NSMutableArray *objArray = [[NSMutableArray alloc] initWithCapacity:0]; 

    //adding objects to objArray 
    . 
    . 
    //operation with objArray finished 

    // 1. should objArray be released here? 
    }]; 

正如你不会需要它的后块(它只对异步操作的持续时间有意义),所以最终在你使用它之后释放它。或者,你可以简单地:

 NSMutableArray *objArray = [NSMutableArray array]; 

而在这种情况下,你不需要释放它。

如果是同步呼叫,则应该在release之后。


注:我假设你的块,这意味着它是有道理的块开始之前要创建上使用之前填充NSMutableArray

异步方法:

-(void) doSomething 
{ 
    // Remove the `__block` qualifier, you want the block to `retain` it so it 
    // can live after the `doSomething` method is destroyed 
    NSMutableArray *objArray = // created with something useful 

    [self performOperationWithBlock:^(void) 
    { 
     // You do something with the objArray, like adding new stuff to it (you are modyfing it). 
     // Since you have the __block qualifier (in non-ARC it has a different meaning, than in ARC) 
     // Finally, you need to be a good citizen and release it. 
    }]; 

    // By the the time reaches this point, the block might haven been, or not executed (it's an async call). 
    // With this in mind, you cannot just release the array. So you release it inside the block 
    // when the work is done 
} 

同步方法

它假设你需要立即的结果,这是有道理的,当你做进一步的工作与阵列,后挡已执行,因此:

-(void) doSomething 
{ 
    // Keep `__block` keyword, you don't want the block to `retain` as you 
    // will release it after 
    __block NSMutableArray *objArray = // created with something useful 

    [self performOperationWithBlock:^(void) 
    { 
     // You do something with the objArray, like adding new stuff to it (you are modyfing it). 
    }]; 
    // Since it's a sync call, when you reach this point, the block has been executed and you are sure 
    // that at least you won't be doing anything else inside the block with Array, so it's safe to release it 

    // Do something else with the array 

    // Finally release it: 

    [objArray release]; 
} 
+0

可以说我需要在块外部填充这个数组,那么图片会是什么? –

+0

@devgr,它是异步还是同步块调用? – Peres

+0

我需要了解透视同步和异步。 –

1

你应该在performOperationWithBlock:方法完成后释放它,在我看来,如果方法是同步(即在与调用线程相同的线程上工作)。

如果该方法是异步那么它应该在块内释放。

0

如果您不使用ARC,则应在不再需要时释放阵列。根据您添加的评论,并假设doSomething方法不对块外的数组执行任何操作,它应该在您的1.标记处。

0

选项2.在[self performOperationWithBlock:...]之后释放它。 Block将自行保留并释放您的objArray。块内部释放是危险的:块可以执行两次,然后objArray将被释放两次,但它应该释放一次。所以,只有一个选项左:2

+0

不管__block变量是保留还是复制,我都有点困惑。 –

+0

@devgr,所有对象都被保留在块中。复制没有意义,并不是所有的类都支持复制。不好,你没有标出正确的答案。 –

+0

仅当您复制块时才保留对象。 –