Skip to content

Commit 95e5260

Browse files
authored
fix: Memory leak on darwin (#1969)
# Description We at Elredo sometime run into weird issues regarding audio playing using this library. So I started to investigate and I found out that there might be few memory leaks in the library.
1 parent 65c8f37 commit 95e5260

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

packages/audioplayers_darwin/darwin/audioplayers_darwin/Sources/audioplayers_darwin/AudioplayersDarwinPlugin.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ public class AudioplayersDarwinPlugin: NSObject, FlutterPlugin {
7373
}
7474

7575
public func detachFromEngine(for registrar: FlutterPluginRegistrar) {
76-
Task { @MainActor in
76+
Task { @MainActor [weak self] in
77+
guard let self = self else {
78+
return
79+
}
7780
await disposePlayers()
7881
self.globalMethods.setMethodCallHandler(nil)
7982
self.globalEvents.dispose()

packages/audioplayers_darwin/darwin/audioplayers_darwin/Sources/audioplayers_darwin/WrappedMediaPlayer.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,14 @@ enum ReleaseMode: String {
197197
) async throws {
198198
try await withCheckedThrowingContinuation { continuation in
199199
playerItemStatusObservation = playerItem.observe(\AVPlayerItem.status) {
200-
(playerItem, change) in
200+
[weak self] (playerItem, change) in
201+
guard let self = self else {
202+
return
203+
}
201204
let status = playerItem.status
202205
self.eventHandler.onLog(message: "player status: \(status), change: \(change)")
203206

204-
switch playerItem.status {
207+
switch status {
205208
case .readyToPlay:
206209
continuation.resume()
207210
case .failed:

0 commit comments

Comments
 (0)