From ebdd59aff8381f1c836cf2f1c2db7df256fdaf5d Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Sun, 10 May 2020 21:29:32 -0700 Subject: [PATCH] Fabric: Initializing EventBeat::OwnerBox with non-null value ahead of time Summary: We use `EventBeat::OwnerBox` in the `EventBeat` infra to represent weak ownership of classes that contain that, so those classes can ensure own life-time at some critical points. Before this diff, we stored a pointer to EventDispatcher right after we create it. However, some components that we build require to have a valid pointer from the beginning (even before the EventDispatcher is created). To satisfy this requirement, we create a dummy pointer first and use it as an owner, then we merge (share control block) the pointer to an EventDispatcher into that. It works because the owner pointer never gets dereferenced (it's `void *`), so it does not matter which object it represents while it represents correct ownership. In the future, this approach will allow us to remove the concept of `OwnerBox` completely and use just `std::weak_ptr` instead. Changelog: [Internal] Fabric-specific internal change. Reviewed By: mdvacca Differential Revision: D21441109 fbshipit-source-id: 7457c77bd31cd577f38a26e28e27eb7e33b6ad24 --- ReactCommon/fabric/scheduler/Scheduler.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ReactCommon/fabric/scheduler/Scheduler.cpp b/ReactCommon/fabric/scheduler/Scheduler.cpp index e9db21ae7e7..9a402251256 100644 --- a/ReactCommon/fabric/scheduler/Scheduler.cpp +++ b/ReactCommon/fabric/scheduler/Scheduler.cpp @@ -32,6 +32,11 @@ Scheduler::Scheduler( auto uiManager = std::make_shared(); auto eventOwnerBox = std::make_shared(); + // A dummy pointer to share a control block (and life-time) with + // an actual `owner` later. + auto owner = std::make_shared(false); + eventOwnerBox->owner = owner; + auto eventPipe = [uiManager]( jsi::Runtime &runtime, const EventTarget *eventTarget, @@ -47,14 +52,15 @@ Scheduler::Scheduler( uiManager->updateState(stateUpdate); }; - eventDispatcher_ = std::make_shared( + auto eventDispatcher = std::make_unique( eventPipe, statePipe, schedulerToolbox.synchronousEventBeatFactory, schedulerToolbox.asynchronousEventBeatFactory, eventOwnerBox); - eventOwnerBox->owner = eventDispatcher_; + eventDispatcher_ = + std::shared_ptr(owner, eventDispatcher.release()); componentDescriptorRegistry_ = schedulerToolbox.componentRegistryFactory( eventDispatcher_, schedulerToolbox.contextContainer);