Memory Leak in AVAudioPlayer in Simulator only

I have a memory leak, when using AVAudioPlayer. I managed to narrow down the issue into a very simple app, which code I paste in at the end.

The memory leak start immediately when I start playing sound, but only in the emylator. On the real iPhone there is no memory leak.

The memory leak on the Simulator looks like this:

import SwiftUI
import AVFoundation

struct ContentView_Audio: View {
    var sound: AVAudioPlayer?
    
    init() {
        guard let path = Bundle.main.path(forResource: "cd201", ofType: "mp3") else { return }
        let url = URL(fileURLWithPath: path)

        do {
            try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers])
        } catch {
            return
        }
        do {
            try AVAudioSession.sharedInstance().setActive(true)
        } catch {
            return
        }
        
        do {
            sound = try AVAudioPlayer(contentsOf: url)
        } catch {
            return
        }
    }

    var body: some View {
        HStack {
            Button {
                playSound()
            } label: {
                ZStack {
                    Circle()
                        .fill(.mint.opacity(0.3))
                        .frame(width: 44, height: 44)
                        .shadow(radius: 8)
                    Image(systemName: "play.fill")
                        .resizable()
                        .frame(width: 20, height: 20)
                }
            }
            .padding()
            
            Button {
                stopSound()
            } label: {
                ZStack {
                    Circle()
                        .fill(.mint.opacity(0.3))
                        .frame(width: 44, height: 44)
                        .shadow(radius: 8)
                    Image(systemName: "stop.fill")
                        .resizable()
                        .frame(width: 20, height: 20)
                }
            }
            .padding()
        }
    }
    
    
    private func playSound() {
        guard sound != nil else { return }
        sound?.volume = 1
//        sound?.numberOfLoops = -1
        sound?.play()
    }
    
    
    func stopSound() {
        sound?.stop()
   }

}

You should probably raise this as a bug in the usual way. It won't really get progressed if it's only posted in these Developer Forums.

You need to raise each issue you find separately at https://feedbackassistant.apple.com/ You can post the FB numbers here if you want, so that others can link to them.

Thanks. I have registered the issue. Its number is: FB17062925

Memory Leak in AVAudioPlayer in Simulator only
 
 
Q