diff --git a/shared/apple/MpvPlayer/MpvPlayerCoreBase.swift b/shared/apple/MpvPlayer/MpvPlayerCoreBase.swift index 77dc0514..ccbb51f5 100644 --- a/shared/apple/MpvPlayer/MpvPlayerCoreBase.swift +++ b/shared/apple/MpvPlayer/MpvPlayerCoreBase.swift @@ -74,6 +74,7 @@ class MpvPlayerCoreBase: NSObject { var isDisposing = false var isPipActive = false var isBackgrounded = false + private var wakeupCallbackContext: UnsafeMutableRawPointer? private var cachedHDREnabled = true private var cachedLastSigPeak = 0.0 private var cachedDoviProfile: Int64 = 0 @@ -218,6 +219,11 @@ class MpvPlayerCoreBase: NSObject { return false } + // mpv stores this context without retaining it. Retain manually so the + // Swift core cannot deallocate while mpv can still fire wakeup callbacks. + let wakeupContext = Unmanaged.passRetained(self).toOpaque() + wakeupCallbackContext = wakeupContext + mpv_set_wakeup_callback( mpv, { context in @@ -225,7 +231,7 @@ class MpvPlayerCoreBase: NSObject { let core = Unmanaged.fromOpaque(context).takeUnretainedValue() core.readEvents() }, - UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque()) + wakeupContext ) mpv_observe_property(mpv, Self.internalSigPeakObserverId, "video-params/sig-peak", MPV_FORMAT_DOUBLE) @@ -441,13 +447,18 @@ class MpvPlayerCoreBase: NSObject { cacheLock.unlock() let mpvHandle = mpv + let callbackContext = wakeupCallbackContext mpv = nil + wakeupCallbackContext = nil let destroy = { if let mpvHandle { mpv_set_wakeup_callback(mpvHandle, nil, nil) mpv_terminate_destroy(mpvHandle) } + if let callbackContext { + Unmanaged.fromOpaque(callbackContext).release() + } } if destroySynchronously { diff --git a/tvos/Runner.xcodeproj/project.pbxproj b/tvos/Runner.xcodeproj/project.pbxproj index 49be2c59..3d032bc3 100644 --- a/tvos/Runner.xcodeproj/project.pbxproj +++ b/tvos/Runner.xcodeproj/project.pbxproj @@ -712,7 +712,7 @@ repositoryURL = "https://github.com/edde746/MPVKit"; requirement = { kind = revision; - revision = 86dcf97f04e572abb5641e74780251a1d5634ff3; + revision = 8b921258a0e963b574f96a919c26c1d1e185af72; }; }; /* End XCRemoteSwiftPackageReference section */ diff --git a/tvos/Runner.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/tvos/Runner.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 970d1468..c38d2c94 100644 --- a/tvos/Runner.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/tvos/Runner.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -6,7 +6,7 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/edde746/MPVKit", "state" : { - "revision" : "86dcf97f04e572abb5641e74780251a1d5634ff3" + "revision" : "8b921258a0e963b574f96a919c26c1d1e185af72" } } ], diff --git a/tvos/Runner.xcworkspace/xcshareddata/swiftpm/Package.resolved b/tvos/Runner.xcworkspace/xcshareddata/swiftpm/Package.resolved index 970d1468..c38d2c94 100644 --- a/tvos/Runner.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/tvos/Runner.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -6,7 +6,7 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/edde746/MPVKit", "state" : { - "revision" : "86dcf97f04e572abb5641e74780251a1d5634ff3" + "revision" : "8b921258a0e963b574f96a919c26c1d1e185af72" } } ],