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

Commit 28cf0b3

Browse files
committed
Add calls to Dart_NotifyDestroyed when the flutter view is destroyed.
1 parent b6e45ae commit 28cf0b3

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

runtime/runtime_controller.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,19 @@ bool RuntimeController::NotifyIdle(fml::TimePoint deadline) {
235235
return true;
236236
}
237237

238+
bool RuntimeController::NotifyDestroyed() {
239+
std::shared_ptr<DartIsolate> root_isolate = root_isolate_.lock();
240+
if (!root_isolate) {
241+
return false;
242+
}
243+
244+
tonic::DartState::Scope scope(root_isolate);
245+
246+
Dart_NotifyDestroyed();
247+
248+
return true;
249+
}
250+
238251
bool RuntimeController::DispatchPlatformMessage(
239252
std::unique_ptr<PlatformMessage> message) {
240253
if (auto* platform_configuration = GetPlatformConfigurationIfAvailable()) {

runtime/runtime_controller.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,17 @@ class RuntimeController : public PlatformConfigurationClient {
360360
///
361361
virtual bool NotifyIdle(fml::TimePoint deadline);
362362

363+
//----------------------------------------------------------------------------
364+
/// @brief Notify the Dart VM that the attached flutter view has been
365+
/// destroyed. This gives the Dart VM to perform some cleanup
366+
/// activities e.g: perform garbage collection to free up any
367+
/// unused memory.
368+
///
369+
/// NotifyDestroyed is advisory. The VM may or may not perform any clean up
370+
/// activities.
371+
///
372+
virtual bool NotifyDestroyed();
373+
363374
//----------------------------------------------------------------------------
364375
/// @brief Returns if the root isolate is running. The isolate must be
365376
/// transitioned to the running phase manually. The isolate can

shell/common/engine.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,11 @@ void Engine::NotifyIdle(fml::TimePoint deadline) {
266266
runtime_controller_->NotifyIdle(deadline);
267267
}
268268

269+
void Engine::NotifyDestroyed() {
270+
TRACE_EVENT0("flutter", "Engine::NotifyDestroyed");
271+
runtime_controller_->NotifyDestroyed();
272+
}
273+
269274
std::optional<uint32_t> Engine::GetUIIsolateReturnCode() {
270275
return runtime_controller_->GetRootIsolateReturnCode();
271276
}

shell/common/engine.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,13 @@ class Engine final : public RuntimeDelegate, PointerDataDispatcher::Delegate {
554554
///
555555
void NotifyIdle(fml::TimePoint deadline);
556556

557+
//----------------------------------------------------------------------------
558+
/// @brief Notifies the engine that the attached flutter view has been
559+
/// destroyed.
560+
/// This enables the engine to notify the Dart VM so it can do
561+
/// some cleanp activities.
562+
void NotifyDestroyed();
563+
557564
//----------------------------------------------------------------------------
558565
/// @brief Dart code cannot fully measure the time it takes for a
559566
/// specific frame to be rendered. This is because Dart code only

shell/common/shell.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,12 @@ void Shell::OnPlatformViewDestroyed() {
899899
// Overall, the longer term plan is to remove this implementation once
900900
// https://github.com/flutter/flutter/issues/96679 is fixed.
901901
rasterizer_->TeardownExternalViewEmbedder();
902+
903+
auto ui_task = [engine = engine_->GetWeakPtr()] {
904+
if (engine) {
905+
engine->NotifyDestroyed();
906+
}
907+
};
902908
}
903909

904910
// |PlatformView::Delegate|

0 commit comments

Comments
 (0)