Case-ID: 12759603: Memory Leak in UIViewControllerRepresentable and VideoPlayer

Dear Developers and DTS team,

This is writing to seek your expert guidance on a persistent memory leak issue I've discovered while implementing video playback in a SwiftUI application.

Environment Details:

  • iOS 17+, Swift (SwiftUI, AVKit), Xcode 16.2
  • Target Devices:
    • iPhone 15 Pro (iOS 18.3.2)
    • iPhone 16 Plus (iOS 18.3.2)

Detailed Issue Description: I am experiencing consistent memory leaks when using UIViewControllerRepresentable with AVPlayerViewController for FullscreenVideoPlayer and native VideoPlayer during video playback termination.

Code Context: I have implemented the following approaches:

  • Added static func dismantleUIViewController(: coordinator:)
  • Included deinit in Coordinator
  • Utilized both UIViewControllerRepresentable and native VideoPlayer
/// A custom AVPlayer integrated with AVPlayerViewController for fullscreen video playback.
///
/// - Parameters:
/// - videoURL: The URL of the video to be played.
struct FullscreenVideoPlayer: UIViewControllerRepresentable {
// @Binding something for controlling fullscreen
let videoURL: URL?
func makeUIViewController(context: Context) -> AVPlayerViewController {
let controller = AVPlayerViewController()
controller.delegate = context.coordinator
print("AVPlayerViewController created: \(String(describing: controller))")
return controller
}
/// Updates the `AVPlayerViewController` with the provided video URL and playback state.
///
/// - Parameters:
/// - uiViewController: The `AVPlayerViewController` instance to update.
/// - context: The SwiftUI context for updates.
func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {
guard let videoURL else {
print("Invalid videoURL")
return
}
// Initialize AVPlayer if it's not already set
if uiViewController.player == nil || uiViewController.player?.currentItem == nil {
uiViewController.player = AVPlayer(url: videoURL)
print("AVPlayer updated: \(String(describing: uiViewController.player))")
}
// Handle playback state
}
func makeCoordinator() -> Coordinator {
Coordinator(parent: self)
}
static func dismantleUIViewController(_ uiViewController: AVPlayerViewController, coordinator: Coordinator) {
uiViewController.player?.pause()
uiViewController.player?.replaceCurrentItem(with: nil)
uiViewController.player = nil
print("dismantleUIViewController called for \(String(describing: uiViewController))")
}
}
extension FullscreenVideoPlayer {
class Coordinator: NSObject, AVPlayerViewControllerDelegate {
var parent: FullscreenVideoPlayer
init(parent: FullscreenVideoPlayer) {
self.parent = parent
}
deinit {
print("Coordinator deinitialized")
}
}
}
struct ContentView: View {
private let videoURL: URL? = URL(string: "https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.mp4")
var body: some View {
NavigationStack {
Text("My Userful View")
List {
Section("VideoPlayer") {
NavigationLink("FullscreenVideoPlayer") {
FullscreenVideoPlayer(videoURL: videoURL)
.frame(height: 500)
}
NavigationLink("Native VideoPlayer") {
VideoPlayer(player: .init(url: videoURL!))
.frame(height: 500)
}
}
}
}
}
}

Reproducibility Steps:

  1. Run application on target devices
  2. Scenario A - FullscreenVideoPlayer:
    • Tap FullscreenVideoPlayer
    • Play video to completion
    • Repeat process 5 times
  3. Scenario B - VideoPlayer:
    • Navigate back to main screen
    • Tap Video Player
    • Play video to completion
    • Repeat process 5 times

Observed Memory Leak Characteristics: Per Iteration (Debug Memory Graph):

  • 4 instances of NSMutableDictionary (Storage) leaked
  • 4 instances of __NSDictionaryM leaked
  • 4 × 112-byte malloc blocks leaked

Cumulative Effects:

  • Debug console prints: "dismantleUIViewController called for <AVPlayerViewController: 0x{String}> Coordinator deinitialized" when navigate back to main screen

  • After multiple iterations, leak instances double

Specific Questions:

  1. What underlying mechanisms are causing these memory leaks in UIViewControllerRepresentable and VideoPlayer?
  2. What are the recommended strategies to comprehensively prevent and resolve these memory management issues?
Case-ID: 12759603: Memory Leak in UIViewControllerRepresentable and VideoPlayer
 
 
Q