Skip to content

Bug Report: std::optional<double> corruption in Release builds #85735

@hyochan

Description

@hyochan

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    c++ interopFeature: Interoperability with C++

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions