Fix a race in PlatformView construction (flutter/engine#3380)

The PlatformView superclass constructor was posting a task to the UI thread
that adds the view to the shell's global list.  This could result in UI thread
operations seeing PlatformView instances that are not fully constructed and do
not yet have an engine.

This was happening in https://github.com/flutter/flutter/issues/7735
This commit is contained in:
Jason Simmons 2017-01-31 17:54:34 -08:00 committed by GitHub
parent 21f38d7a48
commit 23a36caa6f
7 changed files with 18 additions and 4 deletions

View File

@ -18,10 +18,7 @@ namespace shell {
PlatformView::PlatformView(std::unique_ptr<Rasterizer> rasterizer)
: rasterizer_(std::move(rasterizer)),
size_(SkISize::Make(0, 0)),
weak_factory_(this) {
blink::Threads::UI()->PostTask(
[self = GetWeakPtr()] { Shell::Shared().AddPlatformView(self); });
}
weak_factory_(this) {}
PlatformView::~PlatformView() {
blink::Threads::UI()->PostTask([] { Shell::Shared().PurgePlatformViews(); });
@ -37,6 +34,13 @@ void PlatformView::CreateEngine() {
engine_.reset(new Engine(this));
}
// Add this to the shell's list of PlatformVIews.
// Subclasses should call this after the object is fully constructed.
void PlatformView::PostAddToShellTask() {
blink::Threads::UI()->PostTask(
[self = GetWeakPtr()] { Shell::Shared().AddPlatformView(self); });
}
void PlatformView::DispatchPlatformMessage(
ftl::RefPtr<blink::PlatformMessage> message) {
blink::Threads::UI()->PostTask(

View File

@ -70,6 +70,7 @@ class PlatformView {
explicit PlatformView(std::unique_ptr<Rasterizer> rasterizer);
void CreateEngine();
void PostAddToShellTask();
void SetupResourceContextOnIOThreadPerform(
ftl::AutoResetWaitableEvent* event);

View File

@ -104,6 +104,8 @@ PlatformViewAndroid::PlatformViewAndroid()
SetupResourceContextOnIOThread();
UpdateThreadPriorities();
PostAddToShellTask();
}
PlatformViewAndroid::~PlatformViewAndroid() = default;

View File

@ -34,6 +34,8 @@ PlatformViewMac::PlatformViewMac(NSOpenGLView* gl_view)
shell::Shell::Shared().tracing_controller().set_traces_base_path(
[[paths objectAtIndex:0] UTF8String]);
}
PostAddToShellTask();
}
PlatformViewMac::~PlatformViewMac() = default;

View File

@ -282,6 +282,8 @@ PlatformViewIOS::PlatformViewIOS(CAEAGLLayer* layer)
NSUserDomainMask, YES);
shell::Shell::Shared().tracing_controller().set_traces_base_path(
[paths.firstObject UTF8String]);
PostAddToShellTask();
}
PlatformViewIOS::~PlatformViewIOS() = default;

View File

@ -49,6 +49,8 @@ PlatformViewGLFW::PlatformViewGLFW()
});
valid_ = true;
PostAddToShellTask();
}
PlatformViewGLFW::~PlatformViewGLFW() {

View File

@ -12,6 +12,7 @@ namespace shell {
PlatformViewTest::PlatformViewTest()
: PlatformView(std::unique_ptr<Rasterizer>(new NullRasterizer())) {
CreateEngine();
PostAddToShellTask();
}
PlatformViewTest::~PlatformViewTest() = default;