Make Get GetVsyncWaiter return weak_ptr to prevent dangling pointer crash (flutter/engine#32121)

This commit is contained in:
Chris Yang
2022-03-21 11:05:05 -07:00
committed by GitHub
parent 7f4e2bb9ba
commit 840af99590
10 changed files with 51 additions and 23 deletions

View File

@@ -199,8 +199,9 @@ void Animator::Render(std::unique_ptr<flutter::LayerTree> layer_tree) {
std::move(frame_timings_recorder_));
}
const VsyncWaiter& Animator::GetVsyncWaiter() const {
return *waiter_.get();
const std::weak_ptr<VsyncWaiter> Animator::GetVsyncWaiter() const {
std::weak_ptr<VsyncWaiter> weak = waiter_;
return weak;
}
bool Animator::CanReuseLastLayerTree() {

View File

@@ -57,7 +57,7 @@ class Animator final {
void Render(std::unique_ptr<flutter::LayerTree> layer_tree);
const VsyncWaiter& GetVsyncWaiter() const;
const std::weak_ptr<VsyncWaiter> GetVsyncWaiter() const;
//--------------------------------------------------------------------------
/// @brief Schedule a secondary callback to be executed right after the

View File

@@ -557,7 +557,7 @@ void Engine::LoadDartDeferredLibraryError(intptr_t loading_unit_id,
}
}
const VsyncWaiter& Engine::GetVsyncWaiter() const {
const std::weak_ptr<VsyncWaiter> Engine::GetVsyncWaiter() const {
return animator_->GetVsyncWaiter();
}

View File

@@ -870,7 +870,7 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate {
return runtime_controller_.get();
}
const VsyncWaiter& GetVsyncWaiter() const;
const std::weak_ptr<VsyncWaiter> GetVsyncWaiter() const;
private:
// |RuntimeDelegate|

View File

@@ -1915,7 +1915,7 @@ Shell::GetPlatformMessageHandler() const {
return platform_message_handler_;
}
const VsyncWaiter& Shell::GetVsyncWaiter() const {
const std::weak_ptr<VsyncWaiter> Shell::GetVsyncWaiter() const {
return engine_->GetVsyncWaiter();
}

View File

@@ -401,7 +401,7 @@ class Shell final : public PlatformView::Delegate,
const std::shared_ptr<PlatformMessageHandler>& GetPlatformMessageHandler()
const;
const VsyncWaiter& GetVsyncWaiter() const;
const std::weak_ptr<VsyncWaiter> GetVsyncWaiter() const;
private:
using ServiceProtocolHandler =

View File

@@ -5,21 +5,30 @@
#include "flutter/shell/common/variable_refresh_rate_display.h"
#include "flutter/fml/logging.h"
static double GetInitialRefreshRate(
const std::weak_ptr<flutter::VariableRefreshRateReporter>&
refresh_rate_reporter) {
if (auto reporter = refresh_rate_reporter.lock()) {
return reporter->GetRefreshRate();
}
return 0;
}
namespace flutter {
VariableRefreshRateDisplay::VariableRefreshRateDisplay(
DisplayId display_id,
const VariableRefreshRateReporter& refresh_rate_reporter)
: Display(display_id, refresh_rate_reporter.GetRefreshRate()),
const std::weak_ptr<VariableRefreshRateReporter> refresh_rate_reporter)
: Display(display_id, GetInitialRefreshRate(refresh_rate_reporter)),
refresh_rate_reporter_(refresh_rate_reporter) {}
VariableRefreshRateDisplay::VariableRefreshRateDisplay(
const VariableRefreshRateReporter& refresh_rate_reporter)
: Display(refresh_rate_reporter.GetRefreshRate()),
const std::weak_ptr<VariableRefreshRateReporter> refresh_rate_reporter)
: Display(GetInitialRefreshRate(refresh_rate_reporter)),
refresh_rate_reporter_(refresh_rate_reporter) {}
double VariableRefreshRateDisplay::GetRefreshRate() const {
return refresh_rate_reporter_.GetRefreshRate();
return GetInitialRefreshRate(refresh_rate_reporter_);
}
} // namespace flutter

View File

@@ -18,16 +18,16 @@ class VariableRefreshRateDisplay : public Display {
public:
explicit VariableRefreshRateDisplay(
DisplayId display_id,
const VariableRefreshRateReporter& refresh_rate_reporter);
const std::weak_ptr<VariableRefreshRateReporter> refresh_rate_reporter);
explicit VariableRefreshRateDisplay(
const VariableRefreshRateReporter& refresh_rate_reporter);
const std::weak_ptr<VariableRefreshRateReporter> refresh_rate_reporter);
~VariableRefreshRateDisplay() = default;
// |Display|
double GetRefreshRate() const override;
private:
const VariableRefreshRateReporter& refresh_rate_reporter_;
const std::weak_ptr<VariableRefreshRateReporter> refresh_rate_reporter_;
FML_DISALLOW_COPY_AND_ASSIGN(VariableRefreshRateDisplay);
};

View File

@@ -11,19 +11,37 @@ namespace flutter {
namespace testing {
TEST(VariableRefreshRateDisplayTest, ReportCorrectInitialRefreshRate) {
auto refresh_rate_reporter = std::make_unique<TestRefreshRateReporter>(60);
auto display =
flutter::VariableRefreshRateDisplay(*refresh_rate_reporter.get());
auto refresh_rate_reporter = std::make_shared<TestRefreshRateReporter>(60);
auto display = flutter::VariableRefreshRateDisplay(
std::weak_ptr<TestRefreshRateReporter>(refresh_rate_reporter));
ASSERT_EQ(display.GetRefreshRate(), 60);
}
TEST(VariableRefreshRateDisplayTest, ReportCorrectRefreshRateWhenUpdated) {
auto refresh_rate_reporter = std::make_unique<TestRefreshRateReporter>(60);
auto display =
flutter::VariableRefreshRateDisplay(*refresh_rate_reporter.get());
auto refresh_rate_reporter = std::make_shared<TestRefreshRateReporter>(60);
auto display = flutter::VariableRefreshRateDisplay(
std::weak_ptr<TestRefreshRateReporter>(refresh_rate_reporter));
refresh_rate_reporter->UpdateRefreshRate(30);
ASSERT_EQ(display.GetRefreshRate(), 30);
}
TEST(VariableRefreshRateDisplayTest,
Report0IfReporterSharedPointerIsDestroyedAfterDisplayCreation) {
auto refresh_rate_reporter = std::make_shared<TestRefreshRateReporter>(60);
auto display = flutter::VariableRefreshRateDisplay(
std::weak_ptr<TestRefreshRateReporter>(refresh_rate_reporter));
refresh_rate_reporter.reset();
ASSERT_EQ(display.GetRefreshRate(), 0);
}
TEST(VariableRefreshRateDisplayTest,
Report0IfReporterSharedPointerIsDestroyedBeforeDisplayCreation) {
auto refresh_rate_reporter = std::make_shared<TestRefreshRateReporter>(60);
refresh_rate_reporter.reset();
auto display = flutter::VariableRefreshRateDisplay(
std::weak_ptr<TestRefreshRateReporter>(refresh_rate_reporter));
ASSERT_EQ(display.GetRefreshRate(), 0);
}
} // namespace testing
} // namespace flutter

View File

@@ -757,8 +757,8 @@ static void SetEntryPoint(flutter::Settings* settings, NSString* entrypoint, NSS
}
- (void)initializeDisplays {
const flutter::VsyncWaiterIOS& vsync_waiter_ios =
static_cast<const flutter::VsyncWaiterIOS&>(_shell->GetVsyncWaiter());
auto vsync_waiter = std::shared_ptr<flutter::VsyncWaiter>(_shell->GetVsyncWaiter().lock());
auto vsync_waiter_ios = std::static_pointer_cast<flutter::VsyncWaiterIOS>(vsync_waiter);
std::vector<std::unique_ptr<flutter::Display>> displays;
displays.push_back(std::make_unique<flutter::VariableRefreshRateDisplay>(vsync_waiter_ios));
_shell->OnDisplayUpdates(flutter::DisplayUpdateType::kStartup, std::move(displays));