iOS 5 blocks crash only with Release Build

前端 未结 1 579
小蘑菇
小蘑菇 2021-01-25 12:57

I have using blocks and ARC, and found in some situation, iOS only crash in Release build. It was wrong way to write code, like this.

-(IBAction)clickedButtonA:(         


        
相关标签:
1条回答
  • 2021-01-25 13:33

    You're right that you need to add [block copy]. This is because that block is created in the current stack frame (i.e. within clickedButtonA:event:) but then you add it to a dictionary and presumably pull it out later. When you pull it out later and use it, that original stack frame has gone and you will have a pointer to some random memory that might not (most likely won't) actually be the block any more.

    When you copy the block, if it's on the stack currently then it gets copied to the heap and if it's already on the heap then it just retains it. This means that you now have a block which can be passed around between contexts and will be valid.

    The reason that you are only seeing it crash in release mode is because release mode will be turning on compiler optimisation that is completely changing how the stack is handled. Probably you were very lucky in debug mode not to see the problem and was simply a quirk of how your app is designed.

    0 讨论(0)
提交回复
热议问题