4

I have to fix some existing code that builds just fine with LLVM (on iOS) so that it builds with llvm-gcc-4.2 too. I'm done with pretty much everything, except this pattern which is found at a few places:

@property (nonatomic, retain) __block id myProperty;

I suspect the intent here is to allow access to the property from inside a block without retaining self. How can I remove the __block attribute, which gcc doesn't support here, but still achieve the same effect?

user1530597
  • 43
  • 1
  • 3
  • I'm pretty sure you don't need `__block` on properties... it's for stack allocated locals. In any case, just try it. if there are no errors you are fine. – nielsbot Jul 17 '12 at 04:16
  • From the Clang language spec: "In addition to the new Block type we also introduce a new storage qualifier, __block, **for local variables**" cf. http://opensource.apple.com/source/clang/clang-137/src/tools/clang/docs/BlockLanguageSpec.txt – nielsbot Jul 17 '12 at 04:18

2 Answers2

13

I'll suggest you've found a compiler bug, the declaration:

@property (nonatomic, retain) __block id myProperty;

is meaningless. The __block qualifier is used on local (stack allocated) variables so they are passed by reference to blocks, so they can be updated, and are usually[*] stored on the heap rather than the stack.

Therefore the qualifier __block has no meaning on a property declaration which is concerned with object instances, which are heap allocated at all times in Obj-C.

Just drop the __block from the property declarations - for every compiler.

[*] If a block is never copied to the heap a compiler may optimize __block variables and not move those to the heap either.

CRD
  • 52,522
  • 5
  • 70
  • 86
5

just before you use the variable in a block, create a local pointer of type __block. Don't ever use __block in @property declarations.

EG: TypeOfVariable __block *bock_pointer = self.property;

^{ inside the block use bock_pointer }

Jessedc
  • 12,320
  • 3
  • 50
  • 63