-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Open
Labels
c++ interopFeature: Interoperability with C++Feature: Interoperability with C++
Description
Summary
Similar to #84848 (bool), std::optional<double> values are also corrupted in Release builds when accessed via Swift/C++ interop. This appears to be the same underlying issue affecting different types.
Environment
- Xcode 16.1.1, Swift 6.0, macOS Sequoia
- iOS Simulator (arm64)
Problem
Accessing std::optional<double> from C++ structs in Swift:
- Debug: Works correctly
- Release: Returns
nil, wrong values, or-nan
Example
// C++ struct with std::optional<double> properties
// Input: sampleRate = 22050, channels = 2
// BROKEN in Release:
let rate = config.sampleRate.value // Returns nil or -nan
// WORKAROUND (works in Release):
let rate = config.sampleRate.has_value() ? config.sampleRate.pointee : nilRelease Build Output
| Property | Expected | Actual (Release) |
|---|---|---|
sampleRate |
22050.0 |
nil |
channels |
2.0 |
5.0 (wrong!) |
bitRate |
64000.0 |
-nan |
Reproduction
Real-world repo: react-native-nitro-sound
git clone https://github.com/hyochan/react-native-nitro-sound.git
cd react-native-nitro-sound && yarn install
cd example && pod install && cd ..
yarn workspace react-native-nitro-sound-example build:ios
# Run → Record audio → Check with afinfo
# Expected: 22050 Hz, 2 ch | Actual: 8000 Hz, 1 chNotes
- Bug more likely with structs containing many
std::optionalproperties (our struct has 18) - Same workaround as [cxx-interop] An
std::optional<bool>'s.valueis always false in Release mode #84848: use.has_value() ? .pointee : nilinstead of.value - Causes runtime crashes when
-nanis cast toInt
Related
- [cxx-interop] An
std::optional<bool>'s.valueis always false in Release mode #84848 (same issue forstd::optional<bool>) - fix: Fix optional doubles not building in Release mode on Swift mrousavy/nitro#1065
- Audio codec properties not respected in release builds on iOS hyochan/react-native-nitro-sound#741
Metadata
Metadata
Assignees
Labels
c++ interopFeature: Interoperability with C++Feature: Interoperability with C++