Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit c2745a0

Browse files
authored
WeakPtrFactory should be destroyed before any other members. (#29402)
1 parent 2f4af84 commit c2745a0

19 files changed

+191
-181
lines changed

lib/ui/painting/image_decoder.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ class ImageDecoder {
5757
std::shared_ptr<fml::ConcurrentTaskRunner> concurrent_task_runner_;
5858
fml::WeakPtr<IOManager> io_manager_;
5959
fml::WeakPtrFactory<ImageDecoder> weak_factory_;
60-
6160
FML_DISALLOW_COPY_AND_ASSIGN(ImageDecoder);
6261
};
6362

shell/common/engine.h

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -910,29 +910,6 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate {
910910
}
911911

912912
private:
913-
Engine::Delegate& delegate_;
914-
const Settings settings_;
915-
std::unique_ptr<Animator> animator_;
916-
std::unique_ptr<RuntimeController> runtime_controller_;
917-
918-
// The pointer_data_dispatcher_ depends on animator_ and runtime_controller_.
919-
// So it should be defined after them to ensure that pointer_data_dispatcher_
920-
// is destructed first.
921-
std::unique_ptr<PointerDataDispatcher> pointer_data_dispatcher_;
922-
923-
std::string last_entry_point_;
924-
std::string last_entry_point_library_;
925-
std::string initial_route_;
926-
ViewportMetrics viewport_metrics_;
927-
std::shared_ptr<AssetManager> asset_manager_;
928-
bool activity_running_;
929-
bool have_surface_;
930-
std::shared_ptr<FontCollection> font_collection_;
931-
ImageDecoder image_decoder_;
932-
ImageGeneratorRegistry image_generator_registry_;
933-
TaskRunners task_runners_;
934-
fml::WeakPtrFactory<Engine> weak_factory_;
935-
936913
// |RuntimeDelegate|
937914
std::string DefaultRouteName() override;
938915

@@ -981,6 +958,28 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate {
981958

982959
friend class testing::ShellTest;
983960

961+
Engine::Delegate& delegate_;
962+
const Settings settings_;
963+
std::unique_ptr<Animator> animator_;
964+
std::unique_ptr<RuntimeController> runtime_controller_;
965+
966+
// The pointer_data_dispatcher_ depends on animator_ and runtime_controller_.
967+
// So it should be defined after them to ensure that pointer_data_dispatcher_
968+
// is destructed first.
969+
std::unique_ptr<PointerDataDispatcher> pointer_data_dispatcher_;
970+
971+
std::string last_entry_point_;
972+
std::string last_entry_point_library_;
973+
std::string initial_route_;
974+
ViewportMetrics viewport_metrics_;
975+
std::shared_ptr<AssetManager> asset_manager_;
976+
bool activity_running_;
977+
bool have_surface_;
978+
std::shared_ptr<FontCollection> font_collection_;
979+
ImageDecoder image_decoder_;
980+
ImageGeneratorRegistry image_generator_registry_;
981+
TaskRunners task_runners_;
982+
fml::WeakPtrFactory<Engine> weak_factory_; // Must be the last member.
984983
FML_DISALLOW_COPY_AND_ASSIGN(Engine);
985984
};
986985

shell/common/platform_view.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -823,15 +823,14 @@ class PlatformView {
823823
const;
824824

825825
protected:
826+
// This is the only method called on the raster task runner.
827+
virtual std::unique_ptr<Surface> CreateRenderingSurface();
828+
826829
PlatformView::Delegate& delegate_;
827830
const TaskRunners task_runners_;
828-
829831
PointerDataPacketConverter pointer_data_packet_converter_;
830832
SkISize size_;
831-
fml::WeakPtrFactory<PlatformView> weak_factory_;
832-
833-
// This is the only method called on the raster task runner.
834-
virtual std::unique_ptr<Surface> CreateRenderingSurface();
833+
fml::WeakPtrFactory<PlatformView> weak_factory_; // Must be the last member.
835834

836835
private:
837836
FML_DISALLOW_COPY_AND_ASSIGN(PlatformView);

shell/common/pointer_data_dispatcher.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,18 @@ class SmoothPointerDataDispatcher : public DefaultPointerDataDispatcher {
147147
virtual ~SmoothPointerDataDispatcher();
148148

149149
private:
150+
void DispatchPendingPacket();
151+
void ScheduleSecondaryVsyncCallback();
152+
150153
// If non-null, this will be a pending pointer data packet for the next frame
151154
// to consume. This is used to smooth out the irregular drag events delivery.
152155
// See also `DispatchPointerDataPacket` and input_events_unittests.cc.
153156
std::unique_ptr<PointerDataPacket> pending_packet_;
154157
int pending_trace_flow_id_ = -1;
155-
156158
bool is_pointer_data_in_progress_ = false;
157159

160+
// WeakPtrFactory must be the last member.
158161
fml::WeakPtrFactory<SmoothPointerDataDispatcher> weak_factory_;
159-
160-
void DispatchPendingPacket();
161-
162-
void ScheduleSecondaryVsyncCallback();
163-
164162
FML_DISALLOW_COPY_AND_ASSIGN(SmoothPointerDataDispatcher);
165163
};
166164

shell/common/rasterizer.h

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -448,22 +448,6 @@ class Rasterizer final : public SnapshotDelegate {
448448
void DisableThreadMergerIfNeeded();
449449

450450
private:
451-
Delegate& delegate_;
452-
std::unique_ptr<Surface> surface_;
453-
std::unique_ptr<SnapshotSurfaceProducer> snapshot_surface_producer_;
454-
std::unique_ptr<flutter::CompositorContext> compositor_context_;
455-
// This is the last successfully rasterized layer tree.
456-
std::unique_ptr<flutter::LayerTree> last_layer_tree_;
457-
// Set when we need attempt to rasterize the layer tree again. This layer_tree
458-
// has not successfully rasterized. This can happen due to the change in the
459-
// thread configuration. This will be inserted to the front of the pipeline.
460-
std::unique_ptr<flutter::LayerTree> resubmitted_layer_tree_;
461-
fml::closure next_frame_callback_;
462-
bool user_override_resource_cache_bytes_;
463-
std::optional<size_t> max_cache_bytes_;
464-
fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger_;
465-
fml::TaskRunnerAffineWeakPtrFactory<Rasterizer> weak_factory_;
466-
std::shared_ptr<ExternalViewEmbedder> external_view_embedder_;
467451
// |SnapshotDelegate|
468452
sk_sp<SkImage> MakeRasterSnapshot(
469453
std::function<void(SkCanvas*)> draw_callback,
@@ -500,6 +484,24 @@ class Rasterizer final : public SnapshotDelegate {
500484

501485
static bool NoDiscard(const flutter::LayerTree& layer_tree) { return false; }
502486

487+
Delegate& delegate_;
488+
std::unique_ptr<Surface> surface_;
489+
std::unique_ptr<SnapshotSurfaceProducer> snapshot_surface_producer_;
490+
std::unique_ptr<flutter::CompositorContext> compositor_context_;
491+
// This is the last successfully rasterized layer tree.
492+
std::unique_ptr<flutter::LayerTree> last_layer_tree_;
493+
// Set when we need attempt to rasterize the layer tree again. This layer_tree
494+
// has not successfully rasterized. This can happen due to the change in the
495+
// thread configuration. This will be inserted to the front of the pipeline.
496+
std::unique_ptr<flutter::LayerTree> resubmitted_layer_tree_;
497+
fml::closure next_frame_callback_;
498+
bool user_override_resource_cache_bytes_;
499+
std::optional<size_t> max_cache_bytes_;
500+
fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger_;
501+
std::shared_ptr<ExternalViewEmbedder> external_view_embedder_;
502+
503+
// WeakPtrFactory must be the last member.
504+
fml::TaskRunnerAffineWeakPtrFactory<Rasterizer> weak_factory_;
503505
FML_DISALLOW_COPY_AND_ASSIGN(Rasterizer);
504506
};
505507

shell/gpu/gpu_surface_gl.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ class GPUSurfaceGL : public Surface {
5454
bool AllowsDrawingWhenGpuDisabled() const override;
5555

5656
private:
57+
bool CreateOrUpdateSurfaces(const SkISize& size);
58+
59+
sk_sp<SkSurface> AcquireRenderSurface(
60+
const SkISize& untransformed_size,
61+
const SkMatrix& root_surface_transformation);
62+
63+
bool PresentSurface(SkCanvas* canvas);
64+
5765
GPUSurfaceGLDelegate* delegate_;
5866
sk_sp<GrDirectContext> context_;
5967
sk_sp<SkSurface> onscreen_surface_;
@@ -66,16 +74,9 @@ class GPUSurfaceGL : public Surface {
6674
// external view embedder is present.
6775
const bool render_to_surface_ = true;
6876
bool valid_ = false;
69-
fml::TaskRunnerAffineWeakPtrFactory<GPUSurfaceGL> weak_factory_;
70-
71-
bool CreateOrUpdateSurfaces(const SkISize& size);
72-
73-
sk_sp<SkSurface> AcquireRenderSurface(
74-
const SkISize& untransformed_size,
75-
const SkMatrix& root_surface_transformation);
76-
77-
bool PresentSurface(SkCanvas* canvas);
7877

78+
// WeakPtrFactory must be the last member.
79+
fml::TaskRunnerAffineWeakPtrFactory<GPUSurfaceGL> weak_factory_;
7980
FML_DISALLOW_COPY_AND_ASSIGN(GPUSurfaceGL);
8081
};
8182

shell/gpu/gpu_surface_software.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class GPUSurfaceSoftware : public Surface {
3939
// external view embedder is present.
4040
const bool render_to_surface_;
4141
fml::TaskRunnerAffineWeakPtrFactory<GPUSurfaceSoftware> weak_factory_;
42-
4342
FML_DISALLOW_COPY_AND_ASSIGN(GPUSurfaceSoftware);
4443
};
4544

shell/gpu/gpu_surface_vulkan.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ class GPUSurfaceVulkan : public Surface {
5555
const bool render_to_surface_;
5656

5757
fml::WeakPtrFactory<GPUSurfaceVulkan> weak_factory_;
58-
5958
FML_DISALLOW_COPY_AND_ASSIGN(GPUSurfaceVulkan);
6059
};
6160

shell/platform/darwin/ios/framework/Source/FlutterEngine.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ - (void)dealloc {
199199
object:self
200200
userInfo:nil];
201201

202+
// It will be destroyed and invalidate its weak pointers
203+
// before any other members are destroyed.
204+
_weakFactory.reset();
205+
202206
/// nil out weak references.
203207
[_registrars
204208
enumerateKeysAndObjectsUsingBlock:^(id key, FlutterEngineRegistrar* registrar, BOOL* stop) {

shell/platform/darwin/ios/framework/Source/FlutterKeyboardManager.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ - (void)addSecondaryResponder:(nonnull id<FlutterKeySecondaryResponder>)responde
4949
}
5050

5151
- (void)dealloc {
52+
// It will be destroyed and invalidate its weak pointers
53+
// before any other members are destroyed.
54+
_weakFactory.reset();
55+
5256
[_primaryResponders removeAllObjects];
5357
[_secondaryResponders removeAllObjects];
5458
[_primaryResponders release];

0 commit comments

Comments
 (0)