Skip to content

fix: give AudioTrackSource a dispose() to release its native reference - #287

Merged
devopvoid merged 1 commit into
mainfrom
fix/audio-track-source-native-leak
Sep 15, 2026
Merged

devopvoid merged 1 commit into
mainfrom
fix/audio-track-source-native-leak

Conversation

@devopvoid

Copy link
Copy Markdown
Owner

Summary

  • PeerConnectionFactory.createAudioSource() wraps a ref-counted native AudioSourceInterface in a Java AudioTrackSource, following the same JavaFactories::create(env, ptr.release()) pattern used for AudioTrack/VideoTrack/MediaStream/etc. — but unlike those, AudioTrackSource had no dispose()/release() at all, so the one native reference handed to Java could never be released from the Java side.
  • Every call to createAudioSource() leaked one native audio source reference for the life of the process, independent of whether the resulting track, peer connection, or factory were disposed.
  • Adds a dispose() native method to AudioTrackSource (mirroring JNI_MediaStreamTrack.cpp's pattern: Release() the underlying AudioSourceInterface, log a warning if it wasn't the last ref, and null out the handle), with a matching JNI_AudioTrackSource.h/.cpp pair.
  • Updates webrtc-examples (PeerConnectionExample, PeerConnectionManager) and the get-started.md guide's cleanup snippet to dispose the audio source, since they now demonstrate a real API requirement.

Fixes #239 (confirms the leak GPT flagged in the issue's discussion).

Test plan

  • mvn -pl webrtc test — 150/150 pass, no regressions
  • New regression tests in MediaSourceTests covering dispose() both standalone and after the owning track is disposed
  • mvn -pl webrtc test -Pjni-check on the affected test classes — no FATAL ERROR in native method, only the pre-existing harmless exception-check warnings

PeerConnectionFactory.createAudioSource() returns a Java object wrapping
a ref-counted native AudioSourceInterface, but AudioTrackSource (unlike
every other ref-counted wrapper in the API) exposed no dispose()/release(),
so the one reference handed to Java could never be dropped. Every call to
createAudioSource() leaked the native audio source for the life of the
process, regardless of how the resulting track and peer connection were
torn down.
@devopvoid devopvoid mentioned this pull request Sep 15, 2026
@devopvoid
devopvoid merged commit 85e82a3 into main Sep 15, 2026
11 checks passed
@devopvoid
devopvoid deleted the fix/audio-track-source-native-leak branch September 15, 2026 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suspected memory leak

1 participant