EXC_BAD_ACCESS invoking a block

女生的网名这么多〃 提交于 2019-12-01 09:20:57

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!