1

I'm trying to use blocks in a way where I provide a reference to the object which retains the block, as follows:

typedef void(^RunBlock)(__weak Thing *block_owner, ThingFinishBlock finish);

where Thing has a property run_block, of the type RunBlock.

Thing *thing = [Thing thingWithBlock^(Thing *owner, ThingFinishBlock finish) { ... }];

Calling the run_block from within the Thing goes something like this:

__weak typeof(self) this = self;
_finish_block = ^(){ ... }
self.run_block(this, _finish_block);

So what I'm wondering now is, is it safe to define the run_block's first parameter Thing *owner without prefixing it with __weak, or will this cause a retain loop? I'm unsure, as the pointer is already defined as __weak in the typedef, and the given parameter is already __weak.

^(__weak Thing *owner ...){ ... }

As opposed to

^(Thing *owner, ...) { ... }

Thanks!

thejh
  • 44,854
  • 16
  • 96
  • 107
Goos
  • 121
  • 6

1 Answers1

0

No, __weak in parameters is not part of the function type itself.

typedef void(^RunBlock)(__weak Thing *block_owner, ThingFinishBlock finish);

is the same as

typedef void(^RunBlock)(Thing *block_owner, ThingFinishBlock finish);

It's where you implement the block that the __weak in the parameter matters.

Also, I have no idea why you think this has anything to do with retain cycles.

newacct
  • 119,665
  • 29
  • 163
  • 224