diff --git a/ReactCommon/fabric/components/image/ImageComponentDescriptor.h b/ReactCommon/fabric/components/image/ImageComponentDescriptor.h index 8ce4a1582..d62104c8c 100644 --- a/ReactCommon/fabric/components/image/ImageComponentDescriptor.h +++ b/ReactCommon/fabric/components/image/ImageComponentDescriptor.h @@ -24,7 +24,7 @@ class ImageComponentDescriptor final: public: ImageComponentDescriptor(SharedEventDispatcher eventDispatcher, const SharedContextContainer &contextContainer): ConcreteComponentDescriptor(eventDispatcher), - imageManager_(contextContainer->getInstance()) {} + imageManager_(contextContainer->getInstance()) {} void adopt(UnsharedShadowNode shadowNode) const override { ConcreteComponentDescriptor::adopt(shadowNode); diff --git a/ReactCommon/fabric/uimanager/ContextContainer.h b/ReactCommon/fabric/uimanager/ContextContainer.h index cb8209bb2..2dbd0fb30 100644 --- a/ReactCommon/fabric/uimanager/ContextContainer.h +++ b/ReactCommon/fabric/uimanager/ContextContainer.h @@ -20,6 +20,7 @@ using SharedContextContainer = std::shared_ptr; /* * General purpose dependecy injection container. + * Instance types must be copyable. */ class ContextContainer final { @@ -30,11 +31,12 @@ public: * by `{type, key}` pair. */ template - void registerInstance(std::shared_ptr instance, const std::string &key = "") { + void registerInstance(const T &instance, const std::string &key = "") { std::lock_guard lock(mutex_); + instances_.insert({ {std::type_index(typeid(T)), key}, - instance + std::make_shared(instance) }); } @@ -44,9 +46,10 @@ public: * by {type, key} pair. */ template - std::shared_ptr getInstance(const std::string &key = "") const { + T getInstance(const std::string &key = "") const { std::lock_guard lock(mutex_); - return std::static_pointer_cast(instances_.at({std::type_index(typeid(T)), key})); + + return *std::static_pointer_cast(instances_.at({std::type_index(typeid(T)), key})); } private: