This is regarding use of __block for releasing an object inside block;
NSString *str = [SomeObject methodName]; // returned object is autoreleased
[str retain]; //retaining the object since its auto released
Now i will be using this str object inside a block. inside block i will be releasing this object.
Do i need to prefix with like __block NSString *str = .., since iam releasing it inside block?
From the rules i understand that if we want to modify an object inside block we need to prefix with __block. But does this rule applies for releasing also?
No.
__blockis required for variables you want to modify. Releasing an object doesn’t mean modifying a variable. Rather, it means sending areleasemessage to the object in question. Just as with any other message, there’s no reason you can’t sendreleaseto an object instance inside a block, even without the__blockqualifier.You should be aware that for non-
__blockqualified object variables, the block will retain the object. So, while your explicitreleaseinside the block may balance aretainoutside/before the block is invoked, the object won’t actually be released (at least) until the block itself is released, even if no other object has retained it.