diff --git a/packages/react-native/ReactCommon/react/bridging/LongLivedObject.cpp b/packages/react-native/ReactCommon/react/bridging/LongLivedObject.cpp index 9c6d7c38d86f..aa4b1fcdb589 100644 --- a/packages/react-native/ReactCommon/react/bridging/LongLivedObject.cpp +++ b/packages/react-native/ReactCommon/react/bridging/LongLivedObject.cpp @@ -36,11 +36,15 @@ void LongLivedObjectCollection::add(std::shared_ptr so) { void LongLivedObjectCollection::remove(const LongLivedObject* o) { std::scoped_lock lock(collectionMutex_); - for (auto p = collection_.begin(); p != collection_.end(); p++) { - if (p->get() == o) { - collection_.erase(p); - break; - } + // Empty owner: lookup-only alias, with no ownership or control block. + const std::shared_ptr lookupKey{ + std::shared_ptr{}, const_cast(o)}; + + // A shared_ptr hashes and compares by the pointer it stores, so a + // non-owning shared_ptr to `o` finds its entry without walking the set. + auto entry = collection_.find(lookupKey); + if (entry != collection_.end()) { + collection_.erase(entry); } }