Search code examples
iosnsoperationnsblockoperation

NSBlockOperation and objects in the block


Here is the simple code:

// let's assume that I have to allocate this variable with alloc/init
NSString *someString = [[NSString alloc] initWithFormat:"%@", @"someString"];

NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
  [someClass someFunction: someString];
}];

[queue addOperation:op]

[someString release];

This code will crash when nsblockoperation gets ran since someString is released. What is the best practice to prevent this?

Thank you.

EDIT: ARC is not a choice as it's not my decision to make. Any way to get around this in MRC?

EDIT2: What about following code? Would it work?

// let's assume that I have to allocate this variable with alloc/init
NSString *someString = [[NSString alloc] initWithFormat:"%@", @"someString"];

[someString retain]
NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
  [someClass someFunction: someString];
  [someString release]
}];

[queue addOperation:op]

[someString release];

Solution

  • You really should use Automatic Reference Counting, and simplify the code to

    // let's say the variable is allocated with alloc/init
    NSString *someString = @"someString";
    
    NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
      [someClass someFunction: someString];
    }];
    
    [queue addOperation:op]
    

    If you really have to use Manual Reference Counting you can do this:

    // let's assume that I have to allocate this variable with alloc/init
    NSString *someString = [[NSString alloc] initWithFormat:"%@", @"someString"];
    
    NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
      [someClass someFunction: someString];
      [someString release]
    }];
    
    [queue addOperation:op]
    

    I know its just example code, but if it were not you could also do this... ;)

    NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{
      [someClass someFunction:@"someString"];
    }];
    
    [queue addOperation:op]