EXC_BAD_ACCESS invoking a block

后端 未结 2 500
既然无缘
既然无缘 2021-01-14 03:37

UPDATE | I\'ve uploaded a sample project using the panel and crashing here: http://w3style.co.uk/~d11wtq/BlocksCrash.tar.gz (I know the \"Choose...\" button does nothing, I\

相关标签:
2条回答
  • 2021-01-14 04:16

    Try defining your EDNewFilePanel with the __block modifier:

    __block EDNewFilePanel *newFilePanel = [EDNewFilePanel newFilePanel];
    

    This should retain the object when the block is called, which may be after the Panel object is released. As an unrelated side-effect, this will make also make it mutable within the block scope.

    0 讨论(0)
  • 2021-01-14 04:31

    It's a little odd for you to retain a parameter in one method and release it in another, when that object is not an instance variable.

    I would recommend making the completionHandler bit of your beginSheet stuff an instance variable. It's not like you'd be able to display the sheet more than once at a time anyway, and it would be cleaner this way.

    Also, your EXC_BAD_ACCESS is most likely coming from the [handler retain] call in your beginSheet: method. You're probably invoking this method with something like (for brevity):

    [myObject doThingWithCompletionHandler:^{ NSLog(@"done!"); }];
    

    If that's the case, you must -copy the block instead of retaining it. The block, as typed above, lives on the stack. However, if that stack frame is popped off the execution stack, then that block is gone. poof Any attempt to access the block later will result in a crash, because you're trying to execute code that no longer exists and has been replaced by garbage. As such, you must invoke copy on the block to move it to the heap, where it can live beyond the lifetime of the stack frame in which it was created.

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