'In tvos 15 could get a deallocator issue while resetting the avplayer
In my tvos application i reset the avplayer as e.g Appdelegate.myPlayerVC.avplayerviewcontroller.player = nil
It works fine till the 15.1.1tvos version. But in tvos version 15.2 and above we get few crashes which are not replicable. The crash. is like: Foundation - _NSKVONotifyingOriginalClassForIsa.
On analysis i could find an issue with avplayerviewcontroller.player = nil. (Screenshots attached). I tried the apple sample of "PictureInPictureTvos_Sample". Even in that code base on resetting the player I could see a similar issue.
My doubt is , is resetting the player with nil cause any issue in 15.2 and above OS? .Is this the reason for the crash I am getting in my production build. Please help me to resolve the issue.
Solution 1:[1]
I also have this exact same issue - though I have not verified the issue is 100% with tvOS >= 15.2, customers started to report the issue right around the release of tvOS 15.2 thoough. In my case, the crash is very intermittent - sometimes can be hard to reproduce, sometimes can be reproduced multiple times in a row. Crash happens both in release mode and debug mode.
I also have several observers for each stream / player / playerItem:
player.addObserver(self, forKeyPath: "timeControlStatus", options: NSKeyValueObservingOptions.new, context: nil)
playerItem.addObserver(self, forKeyPath: #keyPath(AVPlayerItem.status), options: [.new], context: nil)
player.addPeriodicTimeObserver(forInterval: interval, queue: .main)
The crash happens in transition from stopping one stream and starting a new one. During this transition, I am stopping the current stream with the following:
avPlayer.pause()
avPlayer.replaceCurrentItem(with: nil)
// now remove the current player from AVPlayerViewController:
avPlayerViewController.player = nil
// now null out internal references to player and playerItem objects
avPlayer = nil
avPlayerItem = nil
Here is the stack-trace / crash log - the crash is ALWAYS within AVKit [AVInterstitialController dealloc] on thread 0. There is never any of my app code reported in the stack trace.
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libobjc.A.dylib 0x18d666624 class_getMethodImplementation + 32
1 Foundation 0x18e980f30 _NSKVONotifyingOriginalClassForIsa + 28
2 Foundation 0x18e980f30 _NSKVONotifyingOriginalClassForIsa + 28
3 Foundation 0x18e97cb38 _NSKeyValueObservationInfoGetObservances + 272
4 Foundation 0x18e987c50 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKeys:count:maybeOldValuesDict:maybeNewValuesDict:usingBlock:] + 244
5 Foundation 0x18e988540 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKey:key:key:usingBlock:] + 68
6 Foundation 0x18e981080 _NSSetObjectValueAndNotify + 284
7 AVKit 0x1a45dd6b8 -[AVInterstitialController dealloc] + 32
8 libobjc.A.dylib 0x18d686da8 AutoreleasePoolPage::releaseUntil(objc_object**) + 196
9 libobjc.A.dylib 0x18d686c80 objc_autoreleasePoolPop + 204
10 CoreFoundation 0x18dd652bc _CFAutoreleasePoolPop + 28
11 CoreFoundation 0x18dcc8994 __CFRunLoopPerCalloutARPEnd + 44
12 CoreFoundation 0x18dcc3b5c __CFRunLoopRun + 2596
13 CoreFoundation 0x18dcc2bf4 CFRunLoopRunSpecific + 572
14 GraphicsServices 0x190eb9afc GSEventRunModal + 160
15 UIKitCore 0x1ca50fcd0 -[UIApplication _run] + 1080
16 UIKitCore 0x1ca5150cc UIApplicationMain + 164
My hunch is that there is some new race condition with the player & playerItem observers and removing the player from avPlayerViewController.
So far in my testing, removing this part:
avPlayerViewController.player = nil
I have not seen a crash - but being that the crash is very intermittent I can not be sure this is the appropriate fix. Also, there is no documentation for AVPlayerViewController which says we should not be clearing the player property by setting it to nil.
So far, this is a big mystery. I am trying to get Apple's attention via a Developer Support ticket. If anyone else gets a reply from Apple, please share response here.
This is causing crashes on Apps that are widely deployed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


