问题背景 [color=rgba(0, 0, 0, 0.87)]硬件:R128 软件:FreeRTOS + rtplayer_test(Cedarx)+ AudioSystem 问题复现[color=rgba(0, 0, 0, 0.87)]复现步骤: - rtplayer_test /data/boot.mp3
- 串口输入"l", 循环播放
- 串口输入"b" , 播放器后台执行
具体表现[color=rgba(0, 0, 0, 0.87)]rtplayer_test 循环播放老化音频十几分钟后,音乐停止播放,报错如下: [color=rgba(0, 0, 0, 0.87)] [color=var(--md-typeset-a-color)] 问题分析[color=rgba(0, 0, 0, 0.87)]首次播放时的内存: code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">c906>free==> Round [1] <==Total Heap Size : 1907128 Bytes ( 1862 KB) Free : 547632 Bytes ( 534 KB) Min Free : 536208 Bytes ( 523 KB) List Task MIN Free Stack(unit: word)Task State Priority Stack #************************************************Name State Pri HWM Idx StkCur StkBotadb-shell X 4 394 44 0x87233a0 0x8722650AudioDecode R 4 3872 49 0x874d2c0 0x87459c0IDLE R 0 52 2 0x863dfe0 0x863de40tcpip B 3 470 12 0x8677ae0 0x8676c30Demux B 6 3126 47 0x87333a0 0x872b960usb-hardware-sc B 6 8018 14 0x871a3a0 0x870a630adbd-input B 5 900 24 0x876eb90 0x876cec0amp-admin B 6 4002 11 0x8675ab0 0x866dda0AudioMT2pb B 4 3882 52 0x8779be0 0x8772050AudioRender B 6 3350 50 0x8756950 0x874ed30amp-ser2 B 6 4006 8 0x865d830 0x8655b00amp-ser3 B 6 4006 9 0x8665910 0x865dbe0amp-ser4 B 6 4006 10 0x866d9f0 0x8665cc0hub-main-thread B 6 8082 13 0x870a2b0 0x86fa620adbd-shell-ser- B 4 906 45 0x8726330 0x8724660XPlayer B 4 3912 48 0x8744e10 0x873d220AudioMT2 B 4 1907 18 0x873cb00 0x8738e10RTplayerThread B 4 3989 51 0x876a630 0x8762980CLI B 6 3772 15 0x8722060 0x871a640Tmr Svc B 6 374 3 0x863f070 0x863e320amp-send-task B 6 930 4 0x8642db0 0x8641070amp-recv-task B 6 924 5 0x8644e90 0x8643150adbd-output B 5 843 25 0x8770cd0 0x876f060adb-event B 5 894 46 0x872a300 0x87286d0amp-ser0 B 6 4006 6 0x864d670 0x8645940amp-ser1 B 6 4006 7 0x8655750 0x864da20[color=rgba(0, 0, 0, 0.87)]播放几次后的内存情况: code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">c906>free==> Round [1] <==Total Heap Size : 1907128 Bytes ( 1862 KB) Free : 456992 Bytes ( 446 KB) Min Free : 453440 Bytes ( 442 KB) List Task MIN Free Stack(unit: word)Task State Priority Stack #************************************************Name State Pri HWM Idx StkCur StkBotadb-shell X 4 394 44 0x87235b0 0x8722650AudioDecode R 4 3872 49 0x874d5e0 0x87459c0IDLE R 0 52 2 0x863dfe0 0x863de40tcpip B 3 458 12 0x8677ae0 0x8676c30usb-hardware-sc B 6 8018 14 0x871a3a0 0x870a630adbd-input B 5 900 24 0x876eb90 0x876cec0amp-admin B 6 4002 11 0x8675ab0 0x866dda0AudioMT2pb B 4 3882 52 0x8779be0 0x8772050AudioRender B 6 3350 50 0x8756950 0x874ed30CLI B 6 3772 15 0x8722060 0x871a640Tmr Svc B 6 374 3 0x863f070 0x863e320amp-ser0 B 6 4006 6 0x864d670 0x8645940amp-ser1 B 6 4006 7 0x8655750 0x864da20amp-ser2 B 6 4006 8 0x865d830 0x8655b00amp-ser3 B 6 4006 9 0x8665910 0x865dbe0amp-ser4 B 6 4006 10 0x866d9f0 0x8665cc0hub-main-thread B 6 8082 13 0x870a2b0 0x86fa620XPlayer B 4 3912 48 0x8744e10 0x873d220AudioMT2 B 4 1907 18 0x873cb00 0x8738e10adbd-output B 5 843 25 0x8770cd0 0x876f060amp-recv-task B 6 924 5 0x8644e90 0x8643150Demux B 6 3126 47 0x8733150 0x872b960amp-send-task B 6 924 4 0x8642db0 0x8641070adb-event B 5 890 46 0x872a2a0 0x87286d0adbd-shell-ser- B 4 906 45 0x8726330 0x8724660RTplayerThread B 4 3989 51 0x876a630 0x8762980[color=rgba(0, 0, 0, 0.87)]观察Free项的剩余内存,可发现在老化播放过程中,内存不断减少,存在泄漏。 泄漏点定位[color=rgba(0, 0, 0, 0.87)]可借助memleak工具,定位内存泄漏处。mrtos menuconfig选上memleak code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">-> System components -> aw components -> Memleak Components Support Tina RTOS Memleak # 使能内存泄露分析工具 (16) Tina RTOS Memleak Backtrace Level # 内存泄露分析栈回溯层数 [ ] Tina RTOS Double Free Check [color=rgba(0, 0, 0, 0.87)]memleak用法如下 code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">作用:内存泄露分析用法:memleak 1 使能内存泄露分析,记录所有内存块申请、释放信息memleak 0 关闭内存泄露分析,删除所有内存块的申请、释放信息memleak 1 thread_name1 thread_name2 使能内存泄露分析,记录指定任务的内存块申请、释放信息[color=rgba(0, 0, 0, 0.87)]串口执行memleak 1 [color=rgba(0, 0, 0, 0.87)]音频老化测试(参考复现步骤),音频播放几次后,执行rtpc q 命令退出播放 [color=rgba(0, 0, 0, 0.87)]串口执行memleak 0 [color=rgba(0, 0, 0, 0.87)]关闭内存泄露检测时,会打印可疑的内存泄露点及其回溯信息; [color=rgba(0, 0, 0, 0.87)]有两个地方: code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;"> 007: ptr = 0x08760960, size = 0x00000f00, thread = AudioRender backtrace : 0x08336AEE backtrace : 0x08336B46 backtrace : 0x08396D52 backtrace : 0x084B025E backtrace : 0x08446856 backtrace : 0x084496C6 008: ptr = 0x0875f990, size = 0x00000f00, thread = AudioRender backtrace : 0x08336AEE backtrace : 0x08336B46 backtrace : 0x0839757E backtrace : 0x084B025E backtrace : 0x08446856 backtrace : 0x084496C6[color=rgba(0, 0, 0, 0.87)]callstack 回溯 code" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: inherit; font-family: inherit; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-width: 0px; border-style: initial; border-color: initial; position: absolute; top: 0.5em; right: 0.5em; z-index: 1; width: 1.5em; height: 1.5em; border-radius: 0.1rem; outline: none; outline-offset: 0.1rem; cursor: pointer; transition: color 0.25s ease 0s;">pvPortMalloc at /workspace/freertos/r128/test_0624_rtplayer/rtos-dev/lichee/rtos/kernel/FreeRTOS-orig/Source/portable/MemMang/heap_4.c:658pvPortCalloc at /workspace/freertos/r128/test_0624_rtplayer/rtos-dev/lichee/rtos/kernel/FreeRTOS-orig/Source/portable/MemMang/heap_4.c:577softvol_ap_update_mode at /workspace/freertos/r128/test_0624_rtplayer/rtos-dev/lichee/rtos/components/common/aw/AudioSystem/audio_plugin/softvolume.c:250_AudioTrackStart at //workspace/rtos-r128/lichee/rtos/components/common/aw/AudioSystem/AudioTrack.c:163RTSoundDeviceStart at /workspace/codec_lib/cedarx_rtos/temp/rtos_cedarx/cedarx/rtos_out/libcore/playback/src/rtosSoundControl.c:766startSoundDevice at /workspace/codec_lib/cedarx_rtos/temp/rtos_cedarx/cedarx/rtos_out/libcore/playback/src/audioRenderComponent.c:800 (inlined by) doRender at /workspace/codec_lib/cedarx_rtos/temp/rtos_cedarx/cedarx/rtos_out/libcore/playback/src/audioRenderComponent.c:942 (inlined by) doRender at /workspace/codec_lib/cedarx_rtos/temp/rtos_cedarx/cedarx/rtos_out/libcore/playback/src/audioRenderComponent.c:898[color=rgba(0, 0, 0, 0.87)]通过回溯信息,发现在resample_ap_update_mode处产生了内存泄漏。 根本原因[color=rgba(0, 0, 0, 0.87)]播放器老化过程中,只调用AudioTrackCreate一次,循环播放时会多次调用_AudioTrackStart,最后退出播放才调用AudioTrackDestroy销毁;所以softvol_ap_update_mode这里,老化过程会多次调用到,会有多次分配,但只在退出时AudioTrackDestroy里才销毁。 解决方法[color=rgba(0, 0, 0, 0.87)] 只在AudioTrackCreateWithStream时创建一次,AudioTrackDestroy,测试10小时左右未出现内存泄漏[color=var(--md-typeset-a-color)]
|