起因
某个进程发生内存泄漏,泄漏的不多,但是会持续泄漏
调试方法
- valgrind
参考前一篇博文,使用valgrind来定位问题,运行一段时间以后,结束程序,打印
==4767== 56 (16 direct, 40 indirect) bytes in 1 blocks are definitely lost in loss record 4 of 4
==4767== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4767== by 0x40053F: ft_smalloc() (/lib/libfreetype.so.6.3.16)
初步定位到是freetype的库发生了泄漏问题
- 查看freetype的代码,发现开发者以及预留了debug内存泄漏问题的代码,参考宏
vi include/freetype/config/ftoption.h
#define FT_DEBUG_MEMORY
打开以上的宏,重新编译freetype
ft_mem_debug_init( FT_Memory memory )
//if ( getenv( "FT2_DEBUG_MEMORY" ) )
代码中还会查一下runtime的env,看下有设置FT2_DEBUG_MEMORY,才会执行memory debug的代码,我们直接屏蔽掉即可
- 重新启动进程,每次
FT_Done_Memory( FT_Memory memory )
ft_mem_debug_done( FT_Memory memory )
ft_mem_table_destroy( FT_MemTable table )
printf(
"leaked memory block at address %p, size %8ld in (%s:%ld)\n",
node->address, node->size,
FT_FILENAME( node->source->file_name ),
node->source->line_no );
就会打印内存泄漏的情况,例如
leaked memory block at address 0x6d60ce50, size 52 in (/home/aksenxu/Novatek/NT96685/D700/na51000_linux_sdk/na51000_linux_sdk/code/lib/external/freetype-2.3.5/src/base/ftglyph.c:355)
FreeType: total memory allocations = 54401
FreeType: maximum memory footprint = 43687
FreeType.Debug: FreeType: 52 bytes of memory leaked in 1 blocks
结果
查一下自己的代码吧,freetype基本上是没有泄漏的