From 738ef55df4010c46c3da597a7ee9a69a19423ca9 Mon Sep 17 00:00:00 2001 From: William Van Haevre Date: Thu, 21 Nov 2024 12:10:46 +0100 Subject: [PATCH 1/4] Add destroy method to THEOplayer interface --- src/api/player/THEOplayer.ts | 5 +++++ src/internal/adapter/THEOplayerAdapter.ts | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/api/player/THEOplayer.ts b/src/api/player/THEOplayer.ts index eab3f017f..4811c1313 100644 --- a/src/api/player/THEOplayer.ts +++ b/src/api/player/THEOplayer.ts @@ -72,6 +72,11 @@ export interface THEOplayer extends EventDispatcher { */ pause(): void; + /** + * destroy the player. + */ + destroy(): void; + /** * Whether the player is paused. */ diff --git a/src/internal/adapter/THEOplayerAdapter.ts b/src/internal/adapter/THEOplayerAdapter.ts index 6c65d5ff7..d0fc0b086 100644 --- a/src/internal/adapter/THEOplayerAdapter.ts +++ b/src/internal/adapter/THEOplayerAdapter.ts @@ -483,6 +483,11 @@ export class THEOplayerAdapter extends DefaultEventDispatcher im } } + destroy(): void { + if (Platform.OS === 'ios') { + } + } + public get version(): PlayerVersion { return this._playerVersion; } From c36ee8a6b1da04d18b64fb2b4854ed1d5a18e95b Mon Sep 17 00:00:00 2001 From: William Van Haevre Date: Thu, 21 Nov 2024 12:13:11 +0100 Subject: [PATCH 2/4] Trigger native destroyPlayer method on playerAPI --- ios/THEOplayerRCTBridge.m | 4 ++-- ios/THEOplayerRCTPlayerAPI.swift | 9 +++++++++ ios/THEOplayerRCTView.swift | 2 +- src/internal/THEOplayerView.tsx | 8 +------- src/internal/adapter/THEOplayerAdapter.ts | 1 + 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/ios/THEOplayerRCTBridge.m b/ios/THEOplayerRCTBridge.m index 8f11cb4f6..5f795882c 100644 --- a/ios/THEOplayerRCTBridge.m +++ b/ios/THEOplayerRCTBridge.m @@ -51,8 +51,6 @@ @interface RCT_EXTERN_MODULE(THEOplayerRCTViewManager, RCTViewManager) RCT_EXPORT_VIEW_PROPERTY(onNativeCastEvent, RCTDirectEventBlock); -RCT_EXTERN_METHOD(destroy:(nonnull NSNumber *)node); - @end // ---------------------------------------------------------------------------- @@ -114,6 +112,8 @@ @interface RCT_EXTERN_REMAP_MODULE(THEORCTPlayerModule, THEOplayerRCTPlayerAPI, RCT_EXTERN_METHOD(setTextTrackStyle:(nonnull NSNumber *)node textTrackStyle:(NSDictionary)textTrackStyle) +RCT_EXTERN_METHOD(destroyPlayer:(nonnull NSNumber *)node); + @end // ---------------------------------------------------------------------------- diff --git a/ios/THEOplayerRCTPlayerAPI.swift b/ios/THEOplayerRCTPlayerAPI.swift index da57220f0..9d2eff052 100644 --- a/ios/THEOplayerRCTPlayerAPI.swift +++ b/ios/THEOplayerRCTPlayerAPI.swift @@ -356,5 +356,14 @@ class THEOplayerRCTPlayerAPI: NSObject, RCTBridgeModule { } } } + + @objc(destroyPlayer:) + func destroyPlayer(_ node: NSNumber) -> Void { + DispatchQueue.main.async { + if let theView = self.bridge.uiManager.view(forReactTag: node) as? THEOplayerRCTView { + theView.destroyPlayer() + } + } + } } diff --git a/ios/THEOplayerRCTView.swift b/ios/THEOplayerRCTView.swift index 4822d1eed..0a0d53b19 100644 --- a/ios/THEOplayerRCTView.swift +++ b/ios/THEOplayerRCTView.swift @@ -139,7 +139,7 @@ public class THEOplayerRCTView: UIView { // MARK: - Destroy Player - func destroyPlayer() { + public func destroyPlayer() { self.mainEventHandler.destroy() self.textTrackEventHandler.destroy() self.mediaTrackEventHandler.destroy() diff --git a/src/internal/THEOplayerView.tsx b/src/internal/THEOplayerView.tsx index cb765d032..1dfaf696d 100644 --- a/src/internal/THEOplayerView.tsx +++ b/src/internal/THEOplayerView.tsx @@ -144,15 +144,9 @@ export class THEOplayerView extends PureComponent im destroy(): void { if (Platform.OS === 'ios') { + NativePlayerModule.destroyPlayer(this._view.nativeHandle); } } From 44d18ec57e9020b4fe1565525b74fdc084d0dd35 Mon Sep 17 00:00:00 2001 From: William Van Haevre Date: Thu, 21 Nov 2024 12:15:07 +0100 Subject: [PATCH 3/4] Unload wrapping viewController to prevent memory leak on dangling THEOplayerRCTView --- ios/THEOplayerRCTView.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ios/THEOplayerRCTView.swift b/ios/THEOplayerRCTView.swift index 0a0d53b19..a3c0738da 100644 --- a/ios/THEOplayerRCTView.swift +++ b/ios/THEOplayerRCTView.swift @@ -154,6 +154,9 @@ public class THEOplayerRCTView: UIView { self.player?.destroy() self.player = nil if DEBUG_THEOPLAYER_INTERACTION { PrintUtils.printLog(logText: "[NATIVE] THEOplayer instance destroyed.") } + + self.theoPlayerViewController.view = nil + self.theoPlayerViewController.removeFromParent() } func processMetadataTracks(metadataTrackDescriptions: [TextTrackDescription]?) { From fb3118585f732f6deb6e68b7429aaa6b26186589 Mon Sep 17 00:00:00 2001 From: William Van Haevre Date: Thu, 21 Nov 2024 12:17:51 +0100 Subject: [PATCH 4/4] Add changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35eac1a54..b9231cdb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.1.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## Unreleased + +### Fixed + +- Fixed a memory leak on iOS, caused by the wrapping ViewController that was keeping a strong reference to the THEOplayerRCTView. + ## [8.8.1] - 24-11-20 ### Fixed