0xced / XCDYouTubeKit

YouTube video player for iOS, tvOS and macOS
MIT License
2.92k stars 626 forks source link

EXC_BREAKPOINT crash on main thread #482

Closed joshgare closed 4 years ago

joshgare commented 4 years ago

Hi,

First of all thanks for a great library 🎉

Secondly, my firebase crash reporting occasionally reported a crash being caused by XCDYouTubeKit on the main thread. It's probably something I am doing wrong but I wanted to writ euphorias an issue to see if anyone could help me fix the problem / bring attention if it is a wider problem.

Stack trace:

Crashed: com.apple.main-thread
0  libswiftFoundation.dylib       0x1cee1aeec static URL._unconditionallyBridgeFromObjectiveC(_:) + 180
1  My App                         0x1026ef5e8 specialized Video.init(xcdVideo:) + 380 (Video.swift:380)
2  My App                         0x10271872c closure #1 in APIClient.retrieveTrack(videoId:completion:) + 149 (APIClient.swift:149)
3  My App                         0x1027187ac thunk for @escaping @callee_guaranteed (@guaranteed XCDYouTubeVideo?, @guaranteed Error?) -> () + 4309649324 (<compiler-generated>:4309649324)
4  XCDYouTubeKit                  0x103f8bc2c (Missing)
5  Foundation                     0x198cebfa0 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 16
6  Foundation                     0x198bf72c0 -[NSBlockOperation main] + 84
7  Foundation                     0x198cee1fc __NSOPERATION_IS_INVOKING_MAIN__ + 20
8  Foundation                     0x198bf6fc4 -[NSOperation start] + 740
9  Foundation                     0x198ceebf4 __NSOPERATIONQUEUE_IS_STARTING_AN_OPERATION__ + 20
10 Foundation                     0x198cee6c0 __NSOQSchedule_f + 180
11 libdispatch.dylib              0x19859dc8c _dispatch_block_async_invoke2 + 104
12 libdispatch.dylib              0x1985eb524 _dispatch_client_callout + 16
13 libdispatch.dylib              0x19859d5b4 _dispatch_main_queue_callback_4CF$VARIANT$mp + 904
14 CoreFoundation                 0x1988a3748 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
15 CoreFoundation                 0x19889e61c __CFRunLoopRun + 1724
16 CoreFoundation                 0x19889dc34 CFRunLoopRunSpecific + 424
17 GraphicsServices               0x1a29e738c GSEventRunModal + 160
18 UIKitCore                      0x19c9d022c UIApplicationMain + 1932
19 My App                         0x1026ca3b0 main + 26 (AVPlayer+ext.swift:26)
20 libdyld.dylib                  0x198725800 start + 4

Line 380 in Video.swift where is xcdVideo is of type XCDYouTubeVideo:

guard let streamUrl = xcdVideo?.streamURL.absoluteString, let xcdVideo = xcdVideo else { return nil }

Video is initialised like so:

XCDYouTubeClient.default().getVideoWithIdentifier(videoId) { (video, error) in
    if let error = error {
        completion(false, nil, error)
    }
    completion(true, Video(xcdVideo: video), nil)
}

Any help would be greatly appreciated!

pigeon commented 4 years ago

Looking at you stack trace it seems like video may be nil in

completion(true, Video(xcdVideo: video), nil)

The guard above checks if error is not nil but doesn't check if video is not nil

joshgare commented 4 years ago

Hi @pigeon,

Thanks for the heads up but my initialiser for Video allows for video to be nil so I don't think it's that, that is causing the problem.

init?(xcdVideo: XCDYouTubeVideo?) {
       guard let streamUrl = xcdVideo?.streamURL.absoluteString, let xcdVideo = xcdVideo else { return nil }
...

I have truncated the rest of my model initialiser as it's quite large and isn't relevant to the issue.

pigeon commented 4 years ago

I see. another thing I can think about is that streamURL is nil. AFAIR

@property (nonatomic, readonly) NSURL *streamURL;

would be bridged into an implicitly unwrapped optional and it would cause a crash if it's nil.

I haven't come across this property before and use streamURLs dictionary in my code. I then check if HD720 exists and if it does not I fall back to whatever is available.

SoneeJohn commented 4 years ago

I see. another thing I can think about is that streamURL is nil. AFAIR

@property (nonatomic, readonly) NSURL *streamURL;

would be bridged into an implicitly unwrapped optional and it would cause a crash if it's nil.

I haven't come across this property before and use streamURLs dictionary in my code. I then check if HD720 exists and if it does not I fall back to whatever is available.

Thanks for spotting this, I figured that streamURL could never be nil because of the YouTube API. I will add the appropriate nullability annotation.