fix(iOS): Replace uses of CGColorRef
with UIColor to avoid manual memory management
#46847
+53
−74
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary:
Update React Native on iOS to do less manual memory management, by replacing uses of
CGColorRef
withUIColor
, and only callingUIColor.CGColor
when needed. This results in much less manual memory management, which is probably a good thing in 2024 :D. The downside is there is a breaking change: the signature of a method inRCTBorderDrawing
changes.This is a followup to #46797 . After that PR merged and I tested merging React Native macOS to the
0.76-stable
branch cut commit again, I saw even more places I needed to manually callCGColorRetain
/CGColorRelease
. The reason is due to React Native macOS specifics (explained below) and I could update React Native macOS to not need these changes, but I thought I would at least throw up a PR to propose the changes, as it may be good to move away from using Core Graphics' C base API as much as possible.Longer Explanation
With microsoft#2209 , I wrote a shim of UIGraphicsImageRenderer for macOS. The main difference is my shim calls the NSImage API imageWithSize:flipped:drawingHandler: to shim the UIGraphicsImageRenderer API imageWithData:. The difference between the two is that the macOS API copies the block, and executes it when Appkit is about to draw the image, while the iOS API executes the block right away. Because of this, I think I am hitting way more places where
CGColorRef
variables needed to be retained / released. Additionally, I hit more of them when I merge to the 0.76 branch cut commit (this is why I didn't catch it with #46797).Given this constraint, I have a couple of options:
lockFocus
was deprecated forimageWithSize:flipped:drawingHandler:
UIColor
(which ARC will memory manage for us) as much as possible.RCTBorderDrawing
which is a breaking change. I've seen other PRs do this, so maybe its OK?Changelog:
[IOS] [BREAKING] - Replace uses of
CGColorRef
with UIColor to avoid manual memory managementTest Plan:
Launching RNTester's View example (which tests a lot of the border / outline / shadow rendering) does not crash for me on both iOS and macOS.