解决内存泄漏:
交叉编译和使用:
Valgrind交叉编译_那时风起的博客-CSDN博客_valgrind交叉编译
使用说明:
valgrind用来检测内存泄漏时,只有在程序退出时才会有内存统计的输出,没有办法在程序运行过程中进行内存检测。如果程序根本无法正常退出,在退出时没有deinit逻辑,或增加一定的排查难度。但是即使是这样,ctrl + C强行使程序退出时,还是可以看到一些有用信息的,使用valgrind检测程序时,最好使用unstrip的程序,这样子可以看到符号信息。下面是我使用Ctrl + C强制退出的输出:
==28104== HEAP SUMMARY:
==28104== in use at exit: 356,611 bytes in 631 blocks
==28104== total heap usage: 403,566 allocs, 402,935 frees, 154,440,196 bytes allocated
==28104==
==28104== Thread 1:
==28104== 16 bytes in 1 blocks are possibly lost in loss record 22 of 136
==28104== at 0x4842D48: calloc (vg_replace_malloc.c:762)
==28104== by 0xAD1FD: query_dev_add_list (mible_dev.c:801)
==28104== by 0xAD337: query_dev_add (mible_dev.c:835)
==28104== by 0xCBC29: mibeacon_upload (mibeacon_parse.c:243)
==28104== by 0xCBAA3: mibeacon_parse (mibeacon_parse.c:160)
==28104== by 0xB6A5F: mible_beacon_handler (adv_beacon.c:170)
==28104== by 0xA7767: gateway_evt_handler (mible_gateway.c:694)
==28104== by 0x4861EE1: start_thread (in /lib/libpthread-2.23.so)
==28104==
==28104== 24 bytes in 1 blocks are possibly lost in loss record 51 of 136
==28104== at 0x4840128: malloc (vg_replace_malloc.c:309)
==28104== by 0x144B5: arch_os_mutex_create (arch_os.c:102)
==28104== by 0x7A5CD: mible_rpc_agent_register (mible_rpc_agent.c:1224)
==28104== by 0x1700D: arch_rpc_agent_init (arch_rpc_agent.c:857)
==28104== by 0x14123: main (main.c:512)
==28104==
==28104== 24 bytes in 1 blocks are possibly lost in loss record 52 of 136
==28104== at 0x4840128: malloc (vg_replace_malloc.c:309)
==28104== by 0x144B5: arch_os_mutex_create (arch_os.c:102)
==28104== by 0x7A5CD: mible_rpc_agent_register (mible_rpc_agent.c:1224)
==28104== by 0x17055: arch_rpc_agent_init (arch_rpc_agent.c:878)
==28104== by 0x14123: main (main.c:512)
==28104==
==28104== 32 bytes in 1 blocks are possibly lost in loss record 81 of 136
==28104== at 0x4840128: malloc (vg_replace_malloc.c:309)
==28104== by 0x7A603: mible_rpc_agent_register (mible_rpc_agent.c:1227)
==28104== by 0x1700D: arch_rpc_agent_init (arch_rpc_agent.c:857)
==28104== by 0x14123: main (main.c:512)
==28104==
==28104== 32 bytes in 1 blocks are possibly lost in loss record 82 of 136
==28104== at 0x4840128: malloc (vg_replace_malloc.c:309)
==28104== by 0x7A603: mible_rpc_agent_register (mible_rpc_agent.c:1227)
==28104== by 0x17055: arch_rpc_agent_init (arch_rpc_agent.c:878)
==28104== by 0x14123: main (main.c:512)
==28104==
==28104== 940 bytes in 47 blocks are possibly lost in loss record 110 of 136
==28104== at 0x4840128: malloc (vg_replace_malloc.c:309)
==28104== by 0xAC1CF: unknown_dev_add (mible_dev.c:307)
==28104== by 0xCBC63: mibeacon_upload (mibeacon_parse.c:251)
==28104== by 0xCBAA3: mibeacon_parse (mibeacon_parse.c:160)
==28104== by 0xB6A5F: mible_beacon_handler (adv_beacon.c:170)
==28104== by 0xA7767: gateway_evt_handler (mible_gateway.c:694)
==28104== by 0x4861EE1: start_thread (in /lib/libpthread-2.23.so)
==28104==
==28104== 1,760 (960 direct, 800 indirect) bytes in 24 blocks are definitely lost in loss record 115 of 136
==28104== at 0x4842D48: calloc (vg_replace_malloc.c:762)
==28104== by 0x7D8A3: mesh_beacon_insert (mible_monitor.c:189)
==28104== by 0x7E3BD: mesh_beacon_handler (mible_monitor.c:540)
==28104== by 0x7E43B: adv_process_handler (mible_monitor.c:562)
==28104== by 0x7FA67: mible_monitor_thread (mible_monitor.c:1173)
==28104== by 0x4861EE1: start_thread (in /lib/libpthread-2.23.so)
==28104==
==28104== 2,992 bytes in 22 blocks are possibly lost in loss record 118 of 136
==28104== at 0x4842D48: calloc (vg_replace_malloc.c:762)
==28104== by 0x400D21D: allocate_dtv (in /lib/ld-2.23.so)
==28104==
==28104== LEAK SUMMARY:
==28104== definitely lost: 960 bytes in 24 blocks
==28104== indirectly lost: 800 bytes in 20 blocks
==28104== possibly lost: 4,060 bytes in 74 blocks
==28104== still reachable: 350,791 bytes in 513 blocks
==28104== of which reachable via heuristic:
==28104== newarray : 203,063 bytes in 18 blocks
==28104== suppressed: 0 bytes in 0 blocks
==28104== Reachable blocks (those to which a pointer was found) are not shown.
==28104== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==28104==
==28104== Use --track-origins=yes to see where uninitialised values come from
==28104== For lists of detected and suppressed errors, rerun with: -s
==28104== ERROR SUMMARY: 428 errors from 9 contexts (suppressed: 0 from 0)
possibly lost:可以先不用关注了,因为程序不是正常退出的。
definitely lost:肯定泄漏了,(960直接泄漏, 800间接泄漏)。
看堆栈信息,定位问题就可以了