EXC_BAD_ACCESS on customised blocks

若如初见. 提交于 2019-12-22 00:26:49

问题


I defined a block in a class:

BXButton.h

typedef void(^ButtonClicked)(id sender, NSDictionary *userInfo);
@property (assign, nonatomic) ButtonClicked onButtonClicked;
- (void)setClickBlock:(ButtonClicked)buttonClicked;

BXButton.m

- (void)setClickBlock:(ButtonClicked)buttonClicked {
    onButtonClicked = buttonClicked;
}
- (void)internalButtonClicked {
    DLog(@"internal clicked");
    if (self.onButtonClicked) {
        onButtonClicked(self, self.userInfo);
    }
}

And I tried to call like this in a view controller:

[_testButton setClickBlock:^(BXButton *sender, NSDictionary *userInfo) {
        DLog(@"userInfo %@", userInfo);
        [sender startLoading];
        [[BXAPIClient sharedPublicClient]postPath:@"/" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [sender endLoading];
            //[safeSelf stop];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [sender endLoading];
            [self stop];
        }];
    }];

And I always got BAD_ACCESS whenever I try to access [self method] or any variable which is defined outside of the block. Any ideas? Thanks!


回答1:


The reason why you get EXC_BAD_ACCESS is because blocks are created on the stack by default, so if you reference them with an assign property they will simply cease to exist when the stack gets teared down.

In order to fix this you should copy the block into the heap. One way to do that is to use copy instead of assign when defining your property.

Change your declaration to

@property(copy, nonatomic) ButtonClicked onButtonClicked;

and use its setter/getter instead of accessing directly to the ivar when assigning a block

- (void)setClickBlock:(ButtonClicked)buttonClicked {
    self.onButtonClicked = buttonClicked;
}
- (void)internalButtonClicked {
    DLog(@"internal clicked");
    if (self.onButtonClicked) {
        self.onButtonClicked(self, self.userInfo);
    }
}


来源:https://stackoverflow.com/questions/13812692/exc-bad-access-on-customised-blocks

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