From 01ee08a68d16382f26c3f9bb0cc0c5a702d4e7fc Mon Sep 17 00:00:00 2001 From: Seunghyun Min Date: Sun, 5 Nov 2023 16:47:28 +0900 Subject: [PATCH 1/4] Fix memory leak by cancelling subscriptions on dispose --- just_audio/lib/just_audio.dart | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/just_audio/lib/just_audio.dart b/just_audio/lib/just_audio.dart index 77be4ea31..c14070000 100644 --- a/just_audio/lib/just_audio.dart +++ b/just_audio/lib/just_audio.dart @@ -93,6 +93,9 @@ class AudioPlayer { /// subscribe to the new platform's events. StreamSubscription? _playerDataSubscription; + StreamSubscription? _becomingNoisyEventSubscription; + StreamSubscription? _interruptionEventSubscription; + final String _id; final _proxy = _ProxyHttpServer(); AudioSource? _audioSource; @@ -244,10 +247,12 @@ class AudioPlayer { } if (handleInterruptions) { AudioSession.instance.then((session) { - session.becomingNoisyEventStream.listen((_) { + _becomingNoisyEventSubscription = + session.becomingNoisyEventStream.listen((_) { pause(); }); - session.interruptionEventStream.listen((event) { + _interruptionEventSubscription = + session.interruptionEventStream.listen((event) { if (event.begin) { switch (event.type) { case AudioInterruptionType.duck: @@ -1129,6 +1134,8 @@ class AudioPlayer { await _pitchSubject.close(); await _sequenceSubject.close(); await _shuffleIndicesSubject.close(); + await _becomingNoisyEventSubscription?.cancel(); + await _interruptionEventSubscription?.cancel(); } /// Switch to using the native platform when [active] is `true` and using the From aaff56c51209bb03bd8b33e4779590be2c68eecc Mon Sep 17 00:00:00 2001 From: Seunghyun Min Date: Sun, 5 Nov 2023 21:38:26 +0900 Subject: [PATCH 2/4] Fix memory leak on android --- just_audio/lib/just_audio.dart | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/just_audio/lib/just_audio.dart b/just_audio/lib/just_audio.dart index c14070000..10d817eea 100644 --- a/just_audio/lib/just_audio.dart +++ b/just_audio/lib/just_audio.dart @@ -93,6 +93,7 @@ class AudioPlayer { /// subscribe to the new platform's events. StreamSubscription? _playerDataSubscription; + StreamSubscription? _androidAudioAttributesSubscription; StreamSubscription? _becomingNoisyEventSubscription; StreamSubscription? _interruptionEventSubscription; @@ -237,7 +238,7 @@ class AudioPlayer { // Respond to changes to AndroidAudioAttributes configuration. if (androidApplyAudioAttributes && _isAndroid()) { AudioSession.instance.then((audioSession) { - audioSession.configurationStream + _androidAudioAttributesSubscription = audioSession.configurationStream .map((conf) => conf.androidAudioAttributes) .where((attributes) => attributes != null) .cast() @@ -1134,6 +1135,7 @@ class AudioPlayer { await _pitchSubject.close(); await _sequenceSubject.close(); await _shuffleIndicesSubject.close(); + await _androidAudioAttributesSubscription?.cancel(); await _becomingNoisyEventSubscription?.cancel(); await _interruptionEventSubscription?.cancel(); } From baeb000cd0e4164bcbc167fd152de5748616a1c9 Mon Sep 17 00:00:00 2001 From: Seunghyun Min Date: Tue, 29 Oct 2024 23:24:53 +0900 Subject: [PATCH 3/4] Specify types for StreamSubscription --- just_audio/lib/just_audio.dart | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/just_audio/lib/just_audio.dart b/just_audio/lib/just_audio.dart index 031e83292..25222ffee 100644 --- a/just_audio/lib/just_audio.dart +++ b/just_audio/lib/just_audio.dart @@ -97,9 +97,10 @@ class AudioPlayer { /// subscribe to the new platform's events. StreamSubscription? _playerDataSubscription; - StreamSubscription? _androidAudioAttributesSubscription; - StreamSubscription? _becomingNoisyEventSubscription; - StreamSubscription? _interruptionEventSubscription; + StreamSubscription? + _androidAudioAttributesSubscription; + StreamSubscription? _becomingNoisyEventSubscription; + StreamSubscription? _interruptionEventSubscription; final String _id; final _proxy = _ProxyHttpServer(); From f9047375e3b03ce6d8485539a052918cf5814de2 Mon Sep 17 00:00:00 2001 From: Ryan Heise Date: Wed, 30 Oct 2024 12:01:04 +1100 Subject: [PATCH 4/4] Update CHANGELOG --- just_audio/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/just_audio/CHANGELOG.md b/just_audio/CHANGELOG.md index 11275f04f..03c0596e7 100644 --- a/just_audio/CHANGELOG.md +++ b/just_audio/CHANGELOG.md @@ -1,6 +1,7 @@ ## 0.9.42 * Fix dealloc crash on iOS/macOS (@cristian1980). +* Fix Dart memory leak on dispose (@MinSeungHyun). ## 0.9.41