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];
You really should use Automatic Reference Counting, and simplify the code to
If you really have to use Manual Reference Counting you can do this:
I know its just example code, but if it were not you could also do this... ;)