Where to release the __block variable?

I have the following code snippet:

-(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?
}

Should I autorelease objArray?

+4
source share
4 answers

If this is an asynchronous call, it makes sense to create NSMutableArrayinside the actual block:

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

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

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

As you will not need this after the block (this only makes sense during the async operation), so at the end release it after you have used it. Or you can simply:

     NSMutableArray *objArray = [NSMutableArray array];

And in this case, you do not need to release it.

If it is a synchronization call, you should releaseafter the block.


Note. . I assume that you fill NSMutableArraybefore using it in the block, which means that it makes sense to create it before the block starts.

:

-(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 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 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 safe to release it

    // Do something else with the array

    // Finally release it:

    [objArray release];
}
+5

performOperationWithBlock:, , , (.. , ).

, .

+1

ARC, , . , , doSomething , 1..

0

Option 2. Release it after [self performOperationWithBlock:...]. The block will save and free your objArray yourself. Releasing the inner block is dangerous: the block can be executed twice, and then objArray will be released twice, but it must be released once. Thus, there is only one option: 2.

0
source

All Articles