天天看点

valgrind使用记录

valgrind介绍参考文章:

https://blog.csdn.net/sduliulun/article/details/7732906

使用valgrind工具进行内存检测,执行如下命令:

valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all  -v ./tlx_process

遇到的内存泄漏问题以及解决方案:

1.Linux下正确使用getifaddrs()函数避免内存泄露

https://blog.csdn.net/tao_627/article/details/45062781

2.遇到多个可使当前函数退出的情况,在退出前,要释放已申请的资源,可以考虑使用goto语句来整理代码结构

此状况时调用cJSON库时,对解析出来的数据,进行多个函数传参的情况下发生

https://blog.csdn.net/shimazhuge/article/details/8448773

https://www.cnblogs.com/heartchord/p/4795337.html

3.对于创建线程,定时器的内存泄漏,可以根据是否频繁的操作而选择是否进行修改

https://www.xuebuyuan.com/1568906.html

线程频繁创建,未销毁,导致泄漏,需要主动销毁不使用的线程

https://www.cnblogs.com/lidabo/archive/2012/08/15/2640204.html

https://blog.csdn.net/inuyashaw/article/details/53465294

https://www.cnblogs.com/fushou/p/7269564.html

4.valgrind提示有泄漏,但是实际情况却没有的状况

==27558== 88 bytes in 1 blocks are still reachable in loss record 4 of 15

==27558==    at 0x4C29BC3: malloc (vg_replace_malloc.c:299)

==27558==    by 0x5357F8A: [email protected]@GLIBC_2.3.3 (in /usr/lib64/librt-2.17.so)

==27558==    by 0x4077D6: set_timer_up (client_timer.c:28)

==27558==    by 0x40A384: pthread_httpd_server (httpd.c:563)

==27558==    by 0x513FE24: start_thread (in /usr/lib64/libpthread-2.17.so)

==27558==    by 0x565ABAC: clone (in /usr/lib64/libc-2.17.so)

==27558==

经测试,timer_create 没有内存泄漏,但是打开太多,会造成线程创建失败,达到可创建句柄的最大值,只在普通用户下会有此状况,所以,创建定时器后,要记得关闭

参考:https://www.ibm.com/developerworks/cn/linux/l-cn-timers/

https://blog.csdn.net/leo9150285/article/details/8271910

==27558== 116 bytes in 1 blocks are definitely lost in loss record 5 of 15

==27558==    at 0x4C29BC3: malloc (vg_replace_malloc.c:299)

==27558==    by 0x55E6C90: __libc_alloc_buffer_allocate (in /usr/lib64/libc-2.17.so)

==27558==    by 0x566D11E: __resolv_conf_allocate (in /usr/lib64/libc-2.17.so)

==27558==    by 0x566B0F1: __resolv_conf_load (in /usr/lib64/libc-2.17.so)

==27558==    by 0x566C9D7: __resolv_conf_get_current (in /usr/lib64/libc-2.17.so)

==27558==    by 0x566B3E1: __res_vinit (in /usr/lib64/libc-2.17.so)

==27558==    by 0x566C4FA: maybe_init (in /usr/lib64/libc-2.17.so)

==27558==    by 0x566C66D: __resolv_context_get (in /usr/lib64/libc-2.17.so)

==27558==    by 0x56753B9: gethostbyname (in /usr/lib64/libc-2.17.so)

==27558==    by 0x407FA8: http_tcpclient_create (http.c:22)

==27558==    by 0x40850A: http_post (http.c:165)

==27558==    by 0x40E8BD: get_http_heart (init_tlx_process.c:1349)

经测试,gethostbyname 没有内存泄漏

参考:http://www.cnblogs.com/LubinLew/p/Linux-gethostbyname.html

继续阅读