Crash when CLLocationUpdate.liveUpdates is called while requesting authorization by CLServiceSession

I watched this video and programmed with this sample code as a reference.

The application crashed when I toggled the switch with the following code.

//
//  ContentView.swift
//  LiveUpdatesInCoreLocation
//

import os
import SwiftUI
import CoreLocation

@MainActor
final class ContentViewModel: ObservableObject {
    private let logger = Logger()
    
    @Published
    var updatesStarted: Bool = false {
        didSet {
            updatesStarted ? self.startLocationUpdates() : self.stopLocationUpdates()
        }
    }
    
    func startLocationUpdates() {
        Task {
            let _ = CLServiceSession(authorization: .whenInUse)
            
            for try await update in CLLocationUpdate.liveUpdates() {
                guard self.updatesStarted else { break }
                logger.debug("update location: \(update.location)")
            }
        }
    }
    
    func stopLocationUpdates() {}
}

struct ContentView: View {
    @StateObject private var model = ContentViewModel()
    
    var body: some View {
        VStack {
            Toggle("Location Updates", isOn: $model.updatesStarted)
        }
        .padding()
    }
}

And this is the error that occurred.

Thread 8: EXC_BAD_ACCESS (code=1, address=0x10)
A bad access to memory terminated the process.

If you remove the below code, the error will not occur.

let _ = CLServiceSession(authorization: .whenInUse)

Therefore, I guess that the problem is in calling CLLocationUpdate.liveUpdates while CLServiceSession is requesting authorization. or am I doing something wrong?

My development environment is as follows:

  • Xcode: Version 16.0 beta 2 (16A5171r)
  • Simulator: iPhone 15 Pro - iOS 18.0(22A5297f)
Crash when CLLocationUpdate.liveUpdates is called while requesting authorization by CLServiceSession
 
 
Q