jpsim / JPSVolumeButtonHandler

JPSVolumeButtonHandler provides an easy block interface to hardware volume buttons on iOS devices. Perfect for camera apps!
MIT License
335 stars 90 forks source link

Fix background thread warning #53

Closed joshbernfeld closed 6 years ago

joshbernfeld commented 6 years ago

Fixes the following background thread warning

screen shot 2017-12-22 at 5 47 57 pm

jpsim commented 6 years ago

I wonder what the semantics are for calling dispatch_async() on the main queue from the main thread. In any case, this should be safe. Thanks.

joshbernfeld commented 6 years ago

Ok time for a second take here. It looks like this code only works properly when dispatch_async is performed synchronously. Based of this backtrace, I think whats happening is once dealloc is set in motion it is impossible to prevent self from releasing. The block attempts to retain self, but that doesn't matter and once the block is executed self is gone.

screen shot 2017-12-24 at 11 28 50 pm This was a EXC_BAD_ACCESS crash

I was able to reliably replicate the crash using dispatch_after in order to force async execution. The below code solved the issue. If you would like me to create a new pull request for this please let me know.

- (void)dealloc {
    [self stopHandler];

    MPVolumeView *volumeView = self.volumeView;
    dispatch_async(dispatch_get_main_queue(), ^{
        [volumeView removeFromSuperview];
    });
}
jpsim commented 6 years ago

Oh crap, yes I think you’re right. Please file a new PR.