From a1298d0c45301f68e1d90e8fdd333c3ca47b8a48 Mon Sep 17 00:00:00 2001 From: Matej Knopp Date: Fri, 19 Feb 2021 19:46:34 +0100 Subject: [PATCH] Fix retain cycles in MacOS embedder (flutter/engine#24263) --- .../darwin/macos/framework/Source/FlutterGLCompositor.h | 2 +- .../darwin/macos/framework/Source/FlutterGLCompositor.mm | 8 ++++++++ .../darwin/macos/framework/Source/FlutterMetalRenderer.mm | 2 +- .../macos/framework/Source/FlutterOpenGLRenderer.mm | 2 +- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositor.h b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositor.h index 8f0be73999..c80e9b4e3d 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositor.h +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositor.h @@ -49,7 +49,7 @@ class FlutterGLCompositor { void SetPresentCallback(const PresentCallback& present_callback); private: - const FlutterViewController* view_controller_; + __weak const FlutterViewController* view_controller_; const NSOpenGLContext* open_gl_context_; PresentCallback present_callback_; diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositor.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositor.mm index 125b90bc07..42c770786d 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositor.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterGLCompositor.mm @@ -28,6 +28,10 @@ FlutterGLCompositor::FlutterGLCompositor(FlutterViewController* view_controller, bool FlutterGLCompositor::CreateBackingStore(const FlutterBackingStoreConfig* config, FlutterBackingStore* backing_store_out) { + if (!view_controller_) { + return false; + } + CGSize size = CGSizeMake(config->size.width, config->size.height); if (!frame_started_) { @@ -134,6 +138,10 @@ void FlutterGLCompositor::StartFrame() { } size_t FlutterGLCompositor::CreateCALayer() { + if (!view_controller_) { + return 0; + } + // FlutterGLCompositor manages the lifecycle of content layers. // The id for a CALayer starts at 0 and increments by 1 for // any given frame. diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalRenderer.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalRenderer.mm index ba2034a766..c707aea2ae 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalRenderer.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterMetalRenderer.mm @@ -26,7 +26,7 @@ static bool OnPresentDrawable(FlutterEngine* engine, const FlutterMetalTexture* #pragma mark - FlutterMetalRenderer implementation @implementation FlutterMetalRenderer { - FlutterEngine* _engine; + __weak FlutterEngine* _engine; FlutterView* _flutterView; } diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.mm index 7790e4634e..33b12834a2 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.mm @@ -60,7 +60,7 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine, // A mapping of textureID to internal FlutterExternalTextureGL adapter. NSMutableDictionary* _textures; - FlutterEngine* _flutterEngine; + __weak FlutterEngine* _flutterEngine; } - (instancetype)initWithFlutterEngine:(FlutterEngine*)flutterEngine {