Ky -
Ky -

Reputation: 32093

Why is my if statement being entered when passed a null block?

I'm trying to make a failsafe to guarantee a null block is never attempted to be executed. To do this, I use a custom property getter, which returns the block if it's not null, else it returns an empty block. However! The if statement is always entered! Then, I get an EXC_BAD_ACCESS when it tries to return the block!

It looks like the block isn't defined, as it has a value of 0x0000000000000000, and its __FuncPtr is NULL, so why is this if statement being entered?

A screenshot of Xcode, showing an object called _pinPadCompletionBlock with a value of 0x0000000000000000, as the only conditional in an if statement, and the program counter on the next line. Below this is the inspector wherein __FuncPtr is NULL.

My block is declared as:

@property (nonatomic, assign) void (^pinPadCompletionBlock)(bool);

Upvotes: 0

Views: 120

Answers (1)

Ky -
Ky -

Reputation: 32093

The problem lies in how the property is declared. Blocks cannot be declared as assign. Simply change your declaration to use copy, instead:

@property (nonatomic, copy) void (^pinPadCompletionBlock)(bool);

Additionally, if you look further than the __FuncPtr, you'll see that there are set values inside the block, meaning that the block isn't actually null, despite the debugger readout. So, the code is behaving properly in entering that statement, and I think you'll find that after changing assign to copy, the block's value will be something more obviously non-null when you get to the if statement..

Upvotes: 1

Related Questions