Skip to content

Commit

Permalink
fix: Delay ctor initialization for WithJNIScope
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Mar 12, 2024
1 parent 3b5a958 commit 24c4225
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions package/android/src/main/cpp/WithJNIScope.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ namespace facebook {
namespace jni {

template <typename T> T WithJNIScope(std::function<T()>&& lambda) {
T result;
jni::ThreadScope::WithClassLoader([&result, lambda = std::move(lambda)]() { result = lambda(); });
return std::move(result);
// std::optional delays default constructor
std::optional<T> result;
jni::ThreadScope::WithClassLoader([&result, lambda = std::move(lambda)]() {
// update the optional value
result.emplace(lambda());
});
return std::move(result.value());
}

} // namespace jni
Expand Down

0 comments on commit 24c4225

Please sign in to comment.