Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebRtcAudioRecord.context is leaking reported by leak canary. How to solve?

My webrtc Android native application has issue with WebRtcAudioRecord.context. Leak canary is continuously reporting that WebRtcAudioRecord.class is leaking memory.

I do dispose my webrtc components this way:

 peerConnection?.dispose()
 videoCapture?.dispose()
 videoSource?.dispose()
 factory.dispose()
 PeerConnectionFactory.stopInternalTracingCapture()
 PeerConnectionFactory.shutdownInternalTracer()

But Leak canary still keep reporting that WebRtcAudioRecord.context is leaking. Any idea how to solve this issue?

My application doesn't stream audio is only streaming video.

This is the canary memory leak report:

┬─── │ GC Root: Global variable in native code │ ├─ org.webrtc.audio.WebRtcAudioRecord instance │ Leaking: UNKNOWN │
Retaining 99 B in 2 objects │ context instance of .ui.activities. │ MainActivity with mDestroyed = true │ ↓ WebRtcAudioRecord.context │

╰→ .ui.activities.MainActivity instance ​ Leaking: YES (ObjectWatcher was watching this because .ui.activities.MainActivity received Activity#onDestroy() ​ callback and Activity#mDestroyed is true) ​ Retaining 153.0 kB in 2750 objects ​ key = 33e94f80-0004-4aeb-b351-dab64a8280da ​ watchDurationMillis = 5134 ​ retainedDurationMillis = 134 ​ mApplication instance of .App ​ mBase instance of androidx.appcompat.view.ContextThemeWrapper

METADATA

Build.VERSION.SDK_INT: 29 Build.MANUFACTURER: HUAWEI LeakCanary version: 2.6 App process name: some.app Count of retained yet cleared: 10 KeyedWeakReference instances Stats: LruCache[maxSize=3000,hits=4250,misses=80514,hitRate=5%] RandomAccess[bytes=4071605,reads=80514,travel=36763422162,range=23400016,size=29 171709] Heap dump reason: 8 retained objects, app is visible Analysis duration: 4019 ms

enter image description here

like image 276
Arsenius Avatar asked Sep 07 '25 16:09

Arsenius


1 Answers

I found the reason!

For me,it was

 val options = PeerConnectionFactory.InitializationOptions.builder(context)
            .setEnableInternalTracer(true)
            .setFieldTrials("WebRTC-H264HighProfile/Enabled/")
            .createInitializationOptions()

I was passing the activity context instead of application context here for the builder.

like image 61
Jeffrey Liu Avatar answered Sep 10 '25 11:09

Jeffrey Liu