EXC_BAD_ACCESS crash calling VLCMediaPlayer.play() for the second time.
I am trying to show a MxPEG stream within a SwiftUI app. I am using MobileVLCKit version ~> 3.3.0 in my Podfile. Here is my UIViewRepresentable code:
import SwiftUI
import MobileVLCKit
struct VLCPlayerView: UIViewRepresentable {
var url: String
static let vlcMediaPlayer = VLCMediaPlayer()
func makeUIView(context: Context) -> UIView {
print("Making UIView")
let controller = UIView()
VLCPlayerView.vlcMediaPlayer.drawable = controller
guard let uri = URL(string: url) else {
fatalError("Could not parse URL string.")
}
let media = VLCMedia(url: uri)
media.addOption(":avformat-format=mxg")
VLCPlayerView.vlcMediaPlayer.media = media
VLCPlayerView.vlcMediaPlayer.play()
return controller
}
func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<VLCPlayerView>) {}
static func dismantleUIView(_ uiView: UIView, coordinator: Self.Coordinator) {
stop()
}
static func stop() {
if (isPlaying()) {
print("Stopping playback")
VLCPlayerView.vlcMediaPlayer.stop()
}
}
static func isPlaying() -> Bool {
return VLCPlayerView.vlcMediaPlayer.isPlaying
}
}
And it's displayed in a VStack like this:
ZStack {
VStack {
DetailHeader(
text: "Camera Video",
isLandscape: isLandscape(orientation: orientation))
ZStack {
ZStack {
Color(.black).frame(height: 200, alignment: .center)
ProgressView().scaleEffect(1.5, anchor: .center).progressViewStyle(CircularProgressViewStyle(tint: .gray))
}
VLCPlayerView(url: cameraURL)
.frame(height: 200, alignment: .center)
}
}
}
I arrived at this code from the example in this thread here.
Opening the stream for the first time works, however navigating to the previous screen and coming back causes an EXC_BAD_ACCESS crash. I've tried a bunch of different workarounds for this, including loading the VLCMediaPlayer from a ViewModel and passing it to the VLCPlayerView directly, but all of them crash after trying to load the camera feed a second time.
Stack trace is here:
And the console output is here:
Running on an M1 Mac mini with XCode version 13.2.1.
Clearly it's holding a reference to something that's being released, but I can't find it. Zombie objects are turned on in XCode Scheme settings, and Address Sanitization is on as well.
Any help would be appreciated.