From 834c029e8bf1053cfaa0d96de2fa4e7efd5a52ca Mon Sep 17 00:00:00 2001 From: Vladimir Vlasov Date: Tue, 31 Dec 2024 10:30:35 +0100 Subject: [PATCH 1/3] fix: avoid memory leak on iOS --- ios/Video/RCTVideo.swift | 1 + ios/Video/ReactNativeVideoManager.swift | 18 ++++++++---------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/ios/Video/RCTVideo.swift b/ios/Video/RCTVideo.swift index 3ce69c8c39..2c15dacff0 100644 --- a/ios/Video/RCTVideo.swift +++ b/ios/Video/RCTVideo.swift @@ -256,6 +256,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) + ReactNativeVideoManager.shared.registerView(newInstance: self) #if USE_GOOGLE_IMA _imaAdsManager = RCTIMAAdsManager(video: self, pipEnabled: isPipEnabled) #endif diff --git a/ios/Video/ReactNativeVideoManager.swift b/ios/Video/ReactNativeVideoManager.swift index 5057b393d6..a06608228a 100644 --- a/ios/Video/ReactNativeVideoManager.swift +++ b/ios/Video/ReactNativeVideoManager.swift @@ -6,33 +6,31 @@ import Foundation public class ReactNativeVideoManager: RNVPlugin { - private let expectedMaxVideoCount = 10 + private let expectedMaxVideoCount = 2 // create a private initializer private init() {} public static let shared: ReactNativeVideoManager = .init() - var instanceList: [RCTVideo] = Array() - var pluginList: [RNVPlugin] = Array() + private var instanceCount = 0 + private var pluginList: [RNVPlugin] = Array() /** - * register a new ReactExoplayerViewManager in the managed list + * register a new view */ func registerView(newInstance: RCTVideo) { - if instanceList.count > expectedMaxVideoCount { + if instanceCount > expectedMaxVideoCount { DebugLog("multiple Video displayed ?") } - instanceList.append(newInstance) + instanceCount += 1 } /** - * unregister existing ReactExoplayerViewManager in the managed list + * unregister existing view */ func unregisterView(newInstance: RCTVideo) { - if let i = instanceList.firstIndex(of: newInstance) { - instanceList.remove(at: i) - } + instanceCount -= 1 } /** From dc55c0b9032304f424a16e5ef6ec4cb1b71dcba5 Mon Sep 17 00:00:00 2001 From: Vladimir Vlasov Date: Tue, 31 Dec 2024 10:56:08 +0100 Subject: [PATCH 2/3] refactor: linter fixes --- ios/Video/ReactNativeVideoManager.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ios/Video/ReactNativeVideoManager.swift b/ios/Video/ReactNativeVideoManager.swift index a06608228a..77767f9c7c 100644 --- a/ios/Video/ReactNativeVideoManager.swift +++ b/ios/Video/ReactNativeVideoManager.swift @@ -17,9 +17,9 @@ public class ReactNativeVideoManager: RNVPlugin { private var pluginList: [RNVPlugin] = Array() /** - * register a new view + * register a new view */ - func registerView(newInstance: RCTVideo) { + func registerView(newInstance _: RCTVideo) { if instanceCount > expectedMaxVideoCount { DebugLog("multiple Video displayed ?") } @@ -29,7 +29,7 @@ public class ReactNativeVideoManager: RNVPlugin { /** * unregister existing view */ - func unregisterView(newInstance: RCTVideo) { + func unregisterView(newInstance _: RCTVideo) { instanceCount -= 1 } From dfb35261a30e50767e1efafa965e1ef74c45ef82 Mon Sep 17 00:00:00 2001 From: Vladimir Vlasov Date: Tue, 31 Dec 2024 18:02:58 +0100 Subject: [PATCH 3/3] refactor: remove unnecessary call --- ios/Video/RCTVideo.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/ios/Video/RCTVideo.swift b/ios/Video/RCTVideo.swift index 2c15dacff0..3ce69c8c39 100644 --- a/ios/Video/RCTVideo.swift +++ b/ios/Video/RCTVideo.swift @@ -256,7 +256,6 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) - ReactNativeVideoManager.shared.registerView(newInstance: self) #if USE_GOOGLE_IMA _imaAdsManager = RCTIMAAdsManager(video: self, pipEnabled: isPipEnabled) #endif