AudioContext destructor dereferences a null audioPlayer_ when construction fails before initialize()

未关闭 适合新手
#1,504 0 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

维护者通常 1 天内回复

@maciejmakowski2003 已经在做这个了。

开始于 2026年11月28日。

  • #1509 来自 @maciejmakowski2003 —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
72/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
android, cpp, ios, react-native

调研方向

从 packages/react-native-audio-api/common/cpp/audioapi/core/AudioContext.cpp 开始,查看 isDriverRunning() 和析构函数,并跟踪 BaseAudioContext::getState() 如何调用 isDriverRunning()。在 initialize() 给 audioPlayer_ 赋值之前让构造抛出异常以复现问题,然后确认空指针解引用。当析构函数和 isDriverRunning() 在 audioPlayer_ 为空时是安全的,并且现有音频测试仍然通过时,即完成。

由索引模型根据 Issue 内容生成。

描述

bug
Description

When an AudioContext is destroyed before initialize() has created its platform player, its destructor dereferences the null audioPlayer_ and the app crashes (EXC_BAD_ACCESS at a small offset such as 0x44). A failed construction — a C++ exception thrown while the host object is being built — therefore becomes a native crash instead of a JS error at new AudioContext().

Path (production crash reports, iOS):

AudioAPIModuleInstaller.h:84   make_shared<AudioContextHostObject>(...)
AudioContextHostObject.cpp:18  (construction unwinds)
std::shared_ptr<AudioContext>::~shared_ptr
AudioContext::~AudioContext    (AudioContext.cpp:23)
BaseAudioContext::getState     (BaseAudioContext.cpp:54)
AudioContext::isDriverRunning  → audioPlayer_->isRunning()   // audioPlayer_ == nullptr
IOSAudioPlayer::isRunning      (IOSAudioPlayer.mm:167)       // EXC_BAD_ACCESS

audioPlayer_ is only assigned in AudioContext::initialize(). The same code is on main today (packages/react-native-audio-api/common/cpp/audioapi/core/AudioContext.cpp):

  • ~AudioContext() calls getState(), and stopDriver() when not closed;
  • BaseAudioContext::getState() calls isDriverRunning() unless the state is CLOSED;
  • AudioContext::isDriverRunning() is return audioPlayer_->isRunning(); with no null check (unlike getBaseLatency() next to it, which checks audioPlayer_ == nullptr).

Suggested fix (what we ship as a local patch on 0.13.5):

bool AudioContext::isDriverRunning() const {
  return audioPlayer_ != nullptr && audioPlayer_->isRunning();
}

AudioContext::~AudioContext() {
  // Never initialised: no player, nothing was started, nothing to stop.
  if (audioPlayer_ != nullptr && getState() != ContextState::CLOSED) {
    close();   // stopDriver() on main
  }
}
Steps to reproduce
  1. Make anything throw while the AudioContextHostObject is being constructed, after the AudioContext member exists and before initialize() assigns audioPlayer_ (in production this happens on memory-starved devices; in a debug build, e.g. throw from BaseAudioContext::initialize).
  2. new AudioContext() from JS.
  3. The app crashes in IOSAudioPlayer::isRunning from ~AudioContext instead of new AudioContext() throwing.
Snack or a link to a repository

No standalone reproducer: the path needs construction to fail part-way, which a Snack cannot force without patching the library. The code path above is deterministic once that happens. Happy to open a PR with the two-line fix if that is welcome.

React Native Audio API version

0.13.5 (code unchanged on main / 1.0.1)

React Native version

0.88.0

Platforms

iOS (the destructor is shared C++, so Android is exposed too)

JavaScript runtime

Hermes

Workflow

Expo (prebuild)

Architecture

New Architecture (Fabric)

Build type

Release

Device

Real device

Device model

iPad (9th gen), iPad Air (M2-class); iPadOS 26–27; low free memory (~110 MB)

主要语言
C++
星标
864
派生
103
平均合并
1 天 16 小时
30 天内合并 PR
77

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

software-mansion/react-native-audio-api 的其他 Issue

查看 software-mansion/react-native-audio-api 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。