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 : nil
Release 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 ch
Notes
Related
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
Problem
Accessing
std::optional<double>from C++ structs in Swift:nil, wrong values, or-nanExample
Release Build Output
sampleRate22050.0nilchannels2.05.0(wrong!)bitRate64000.0-nanReproduction
Real-world repo: react-native-nitro-sound
Notes
std::optionalproperties (our struct has 18)std::optional<bool>'s.valueis always false in Release mode #84848: use.has_value() ? .pointee : nilinstead of.value-nanis cast toIntRelated
std::optional<bool>'s.valueis always false in Release mode #84848 (same issue forstd::optional<bool>)