From ae1b5bbb137db77a4275b1e3a3e0dd0e0e61a7cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rulong=20Chen=EF=BC=88=E9=99=88=E6=B1=9D=E9=BE=99=EF=BC=89?= <26625149+0xZOne@users.noreply.github.com> Date: Fri, 9 Feb 2024 05:18:11 +0800 Subject: [PATCH] Fix Core Foundation object leaks. (flutter/engine#50460) This fixes Core Foundation object leaks. ---- Unrelated, just as exciting: Today is the last day before the Chinese New Year, and I wish everyone a Happy New Year of the Dragon, Xin Nian Kuai Le! :) [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../third_party/txt/src/txt/platform_mac.mm | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/engine/src/flutter/third_party/txt/src/txt/platform_mac.mm b/engine/src/flutter/third_party/txt/src/txt/platform_mac.mm index 6cec1dcf68..30fb6eaaa4 100644 --- a/engine/src/flutter/third_party/txt/src/txt/platform_mac.mm +++ b/engine/src/flutter/third_party/txt/src/txt/platform_mac.mm @@ -4,6 +4,7 @@ #include +#include "flutter/fml/platform/darwin/cf_utils.h" #include "flutter/fml/platform/darwin/platform_version.h" #include "third_party/skia/include/ports/SkFontMgr_mac_ct.h" #include "third_party/skia/include/ports/SkTypeface_mac.h" @@ -42,15 +43,15 @@ sk_sp GetDefaultFontManager(uint32_t font_initialization_data) { return mgr; } -CTFontRef MatchSystemUIFont(float desired_weight, float size) { - CTFontRef ct_font( +fml::CFRef MatchSystemUIFont(float desired_weight, float size) { + fml::CFRef ct_font( CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, size, nullptr)); if (desired_weight == kNormalWeightValue) { return ct_font; } - CFMutableDictionaryRef variations(CFDictionaryCreateMutable( + fml::CFRef variations(CFDictionaryCreateMutable( kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)); @@ -58,24 +59,25 @@ CTFontRef MatchSystemUIFont(float desired_weight, float size) { float desired_value, float normal_value) { if (desired_value != normal_value) { - CFNumberRef tag_number( + fml::CFRef tag_number( CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &tag)); - CFNumberRef value_number(CFNumberCreate( + fml::CFRef value_number(CFNumberCreate( kCFAllocatorDefault, kCFNumberFloatType, &desired_value)); CFDictionarySetValue(variations, tag_number, value_number); } }; add_axis_to_variations(kWeightTag, desired_weight, kNormalWeightValue); - CFMutableDictionaryRef attributes(CFDictionaryCreateMutable( + fml::CFRef attributes(CFDictionaryCreateMutable( kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)); CFDictionarySetValue(attributes, kCTFontVariationAttribute, variations); - CTFontDescriptorRef var_font_desc( + fml::CFRef var_font_desc( CTFontDescriptorCreateWithAttributes(attributes)); - return CTFontCreateCopyWithAttributes(ct_font, size, nullptr, var_font_desc); + return fml::CFRef( + CTFontCreateCopyWithAttributes(ct_font, size, nullptr, var_font_desc)); } void RegisterSystemFonts(const DynamicFontManager& dynamic_font_manager) { @@ -96,9 +98,8 @@ void RegisterSystemFonts(const DynamicFontManager& dynamic_font_manager) { // See https://www.wwdcnotes.com/notes/wwdc20/10175/ for Apple's document on // this topic. auto register_weighted_font = [&dynamic_font_manager](const int weight) { - sk_sp large_system_font_weighted = - SkMakeTypefaceFromCTFont((CTFontRef)CFAutorelease( - MatchSystemUIFont(weight, kSFProDisplayBreakPoint))); + sk_sp large_system_font_weighted = SkMakeTypefaceFromCTFont( + MatchSystemUIFont(weight, kSFProDisplayBreakPoint)); if (large_system_font_weighted) { dynamic_font_manager.font_provider().RegisterTypeface( large_system_font_weighted, kSFProDisplayName);