From 224925f8a215b3972132fbd0d7a69861f8f1a616 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Thu, 16 Jan 2025 00:53:51 +0100 Subject: [PATCH] fix: Fix OwningRef/BorrowingRef empty copy constructo --- .../cpp/utils/BorrowingReference.hpp | 6 ++++-- .../cpp/utils/OwningReference.hpp | 6 ++++-- .../react-native-nitro-modules/cpp/views/CachedProp.hpp | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/react-native-nitro-modules/cpp/utils/BorrowingReference.hpp b/packages/react-native-nitro-modules/cpp/utils/BorrowingReference.hpp index d0fe37bb0..41613c52f 100644 --- a/packages/react-native-nitro-modules/cpp/utils/BorrowingReference.hpp +++ b/packages/react-native-nitro-modules/cpp/utils/BorrowingReference.hpp @@ -32,8 +32,10 @@ class BorrowingReference final { BorrowingReference(const BorrowingReference& ref) : _value(ref._value), _isDeleted(ref._isDeleted), _strongRefCount(ref._strongRefCount), _weakRefCount(ref._weakRefCount), _mutex(ref._mutex) { - // increment ref count after copy - (*_weakRefCount)++; + if (_weakRefCount != nullptr) { + // increment ref count after copy + (*_weakRefCount)++; + } } BorrowingReference(BorrowingReference&& ref) diff --git a/packages/react-native-nitro-modules/cpp/utils/OwningReference.hpp b/packages/react-native-nitro-modules/cpp/utils/OwningReference.hpp index ae7c588d3..beeeb4b96 100644 --- a/packages/react-native-nitro-modules/cpp/utils/OwningReference.hpp +++ b/packages/react-native-nitro-modules/cpp/utils/OwningReference.hpp @@ -39,8 +39,10 @@ class OwningReference final { OwningReference(const OwningReference& ref) : _value(ref._value), _isDeleted(ref._isDeleted), _strongRefCount(ref._strongRefCount), _weakRefCount(ref._weakRefCount), _mutex(ref._mutex) { - // increment ref count after copy - (*_strongRefCount)++; + if (_strongRefCount != nullptr) { + // increment ref count after copy + (*_strongRefCount)++; + } } OwningReference(OwningReference&& ref) diff --git a/packages/react-native-nitro-modules/cpp/views/CachedProp.hpp b/packages/react-native-nitro-modules/cpp/views/CachedProp.hpp index 91e8a9709..6e1d5fdc9 100644 --- a/packages/react-native-nitro-modules/cpp/views/CachedProp.hpp +++ b/packages/react-native-nitro-modules/cpp/views/CachedProp.hpp @@ -32,10 +32,10 @@ namespace margelo::nitro { // jsi::Value hasn't changed - no need to convert it again! return oldProp; } - T converted = JSIConverter::fromJSI(runtime, value); + auto converted = JSIConverter::fromJSI(runtime, value); auto cache = JSICache::getOrCreateCache(runtime); auto cached = cache.makeShared(jsi::Value(runtime, value)); - return CachedProp(std::move(converted), cached, /* isDirty */ true); + return CachedProp(std::move(converted), std::move(cached), /* isDirty */ true); } };