iOS 15.0 regression: AVPlayerViewController player instance is leaked when replaced

Originator:defagos
Number:rdar://FB9940777 Date Originated:Mar 3, 2022
Status:Open Resolved:
Product:AVKit Product Version:iOS
Classification:Bug Reproducible:Always
 
Basic Information
Please provide a descriptive title for your feedback:
iOS 15.0 regression: AVPlayerViewController player instance is leaked when replaced
Which area are you seeing an issue with?
AVKit
What type of feedback are you reporting?
Incorrect/Unexpected Behavior
Description
Please describe the issue and what steps we can take to reproduce it:
Description
-----------

`AVPlayerViewController` has a mutable player property with which the associated `AVPlayer` instance can be changed. Until iOS 15.0 this change was working fine but, since iOS 15.0, updating the player instance actually leaks the previous player instance (which usually continues to simultaneously play in the background).


Instructions to reproduce the problem
--------------------------------------

I created a sample project showing this issue as follows:

1. An `AVPlayerViewController` is instantiated and plays some content.
2. After 3 seconds the player instance is switched with another one playing another stream.

The sample project is attached to this feedback:

1. Extract the associated zip file, build and run the project (on a device or in the simulator).
2. Wait until the second content starts playing.

On iOS 15.0 and above you can hear both contents incorrectly being played at the same time. On prior iOS 14 only the last content can be heard, as expected. Inspection of allocations in Instruments shows that only a single `AVPlayer` instance stays alive prior to iOS 15.0, while 2 instances incorrectly coexist starting with iOS 15.0.

Remark:

The project can also be built for tvOS where the situation seems a bit different:

- On tvOS 14 and 15, content are never simultaneously heard but...
- ... in Instruments, on both tvOS 14 and 15, two persistent `AVPlayer` instances are reported.

It might therefore be valuable to consider what is happening on tvOS as well, as only 1 persistent instance should be expected.


Expected results
-----------------

Switching the player instance associated with an `AVPlayerViewController` correctly releases the previously registered instance, if any.


Actual results
--------------

After replacing the player instance associated with an `AVPlayerViewController`, the previously registered instance is leaked.

Comments

Sample code

Available on GitHub: https://github.com/defagos/radars/tree/master/player-view-controller-leak-ios15


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!