-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: avoid memory leak on iOS #4355
base: master
Are you sure you want to change the base?
fix: avoid memory leak on iOS #4355
Conversation
There might be a bug in the instance counter logic on both platforms. Please take a look at the Android code:
As I understand, a user gets a debug output only after adding the 4th video to the screen: Added the 1st video. Should the condition be @freeboub what do you think? |
ios/Video/RCTVideo.swift
Outdated
@@ -256,6 +256,7 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH | |||
|
|||
required init?(coder aDecoder: NSCoder) { | |||
super.init(coder: aDecoder) | |||
ReactNativeVideoManager.shared.registerView(newInstance: self) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is there 2 init functions 🤔
BTW looks good to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That one with coder is for compatibility with older versions of swift and storyboard - this init in general should be never called
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed this call. I added it just for init/deinit symmetry.
Yes I may do better. I put this, as is, because, from my experience, 2 parallel video decoding on a device is working pretty fine in most of case... but It is not absolutely true. In another fork, I implemented a limitation to ensure we don't use more than 'X' Video in the same time by droping previous instance (for safety). |
@freeboub, in this PR, the maximum value is changed from 10 to 2 to align it with Android. Does this make sense, or should I revert it? |
No that's good, ios is really more robust at this point (but it's not a reason) :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's fine for me
func registerView(newInstance: RCTVideo) { | ||
if instanceList.count > expectedMaxVideoCount { | ||
func registerView(newInstance _: RCTVideo) { | ||
if instanceCount > expectedMaxVideoCount { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this breaking change? If it is let's add "!" to commit -> fix!: avoid memory leak on iOS
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it doesn't break the existing API. But the argument is unused so _
makes the linter happy.
Summary
The PR fixes memory leaks on iOS.
The
RCTVideo
andReactNativeVideoManager
create a reference cycle becauseReactNativeVideoManager
retains the views but never release them.Motivation
Reducing the memory usage in the apps.
Changes
Removes redundant
instanceList
inReactNativeVideoManager
but keep the counter logic. React Native retain the view references so no need to have an extra list.Test plan
RCTVideo
instances in the memory graph.Expected result: only a single
RCTVideo
instance is presented.