今天新的功能测试完成后,担心有内存泄漏问题,所以用valgrind进行下检测。通过valgrind检测还真的发现了一处内存泄漏的问题。
下面详细介绍下我是如何使用的。
1:安装valgrind
先执行下valgrind命令,如果没有valgrind命令,需要安装下。我是用yum install valgrind。
2:使用内存工具
valgrind --tool=memcheck --leak-check=full dns test 1
其中valgrind是命令,--tool=memcheck是使用内存检测工具。 --leak-check=full 对内存完整使用检查。dns是等待检测的可执行程序。dns需要是debug方式编译。test是dns的参数,1也是dns的参数。
3:运行后,跑一下实际用例
查找definitely的关键字样。如果有definitely,那么程序很有可能有内存泄漏。
比如检测我的程序,结果文字如下:
==7590== 54,096 (2,352 direct, 51,744 indirect) bytes in 6 blocks are definitely lost in loss record 148 of 166
==7590== at 0x4C2A593: operator new(unsigned long) (vg_replace_malloc.c:344)
==7590== by 0x72AC2C: TDnsMsgImpl::parse(char**) (dnsmsg.C:111)
==7590== by 0x72D322: TDnsTransaction::recvmsg(TCode) (dnstransaction.C:46)
==7590== by 0x5213C6: processDNSMessage(TCode, int) (feam.C:1808)
==7590== by 0x5313B4: TUDPPort::checkFDSet(fd_set*) (udpport.C:368)
==7590== by 0x531F56: TUDPPortTable::checkFDSet(fd_set*) (udpport.C:605)
==7590== by 0x5291B7: TFEAM::active(int) (feam.C:3845)
==7590== by 0x55CF61: mainLoop() (scmectrl.C:420)
==7590== by 0x56AA61: inRun() (inmain.C:312)
==7590== by 0x56B76F: main (inmain.C:788)
==7590==
==7590== 54,096 (2,352 direct, 51,744 indirect) bytes in 6 blocks are definitely lost in loss record 149 of 166
==7590== at 0x4C2A593: operator new(unsigned long) (vg_replace_malloc.c:344)
==7590== by 0x72AC2C: TDnsMsgImpl::parse(char**) (dnsmsg.C:111)
==7590== by 0x72D544: TDnsTransaction::recvmsg(TCode) (dnstransaction.C:78)
==7590== by 0x5213C6: processDNSMessage(TCode, int) (feam.C:1808)
==7590== by 0x5313B4: TUDPPort::checkFDSet(fd_set*) (udpport.C:368)
==7590== by 0x531F56: TUDPPortTable::checkFDSet(fd_set*) (udpport.C:605)
==7590== by 0x5291B7: TFEAM::active(int) (feam.C:3845)
==7590== by 0x55CF61: mainLoop() (scmectrl.C:420)
==7590== by 0x56AA61: inRun() (inmain.C:312)
==7590== by 0x56B76F: main (inmain.C:788)
==7590==
==7590== 63,112 (2,744 direct, 60,368 indirect) bytes in 7 blocks are definitely lost in loss record 153 of 166
==7590== at 0x4C2A593: operator new(unsigned long) (vg_replace_malloc.c:344)
==7590== by 0x72C495: dnsGetXMLElem(char const*, char const*) (dnsmsg.C:453)
==7590== by 0x72AC8B: TDnsMsgImpl::parse(char**) (dnsmsg.C:116)
==7590== by 0x72D322: TDnsTransaction::recvmsg(TCode) (dnstransaction.C:46)
==7590== by 0x5213C6: processDNSMessage(TCode, int) (feam.C:1808)
==7590== by 0x5313B4: TUDPPort::checkFDSet(fd_set*) (udpport.C:368)
==7590== by 0x531F56: TUDPPortTable::checkFDSet(fd_set*) (udpport.C:605)
==7590== by 0x5291B7: TFEAM::active(int) (feam.C:3845)
==7590== by 0x55CF61: mainLoop() (scmectrl.C:420)
==7590== by 0x56AA61: inRun() (inmain.C:312)
==7590== by 0x56B76F: main (inmain.C:788)
==7590==
==7590== 63,112 (2,744 direct, 60,368 indirect) bytes in 7 blocks are definitely lost in loss record 154 of 166
==7590== at 0x4C2A593: operator new(unsigned long) (vg_replace_malloc.c:344)
==7590== by 0x72C495: dnsGetXMLElem(char const*, char const*) (dnsmsg.C:453)
==7590== by 0x72AC8B: TDnsMsgImpl::parse(char**) (dnsmsg.C:116)
==7590== by 0x72D544: TDnsTransaction::recvmsg(TCode) (dnstransaction.C:78)
==7590== by 0x5213C6: processDNSMessage(TCode, int) (feam.C:1808)
==7590== by 0x5313B4: TUDPPort::checkFDSet(fd_set*) (udpport.C:368)
==7590== by 0x531F56: TUDPPortTable::checkFDSet(fd_set*) (udpport.C:605)
==7590== by 0x5291B7: TFEAM::active(int) (feam.C:3845)
==7590== by 0x55CF61: mainLoop() (scmectrl.C:420)
==7590== by 0x56AA61: inRun() (inmain.C:312)
==7590== by 0x56B76F: main (inmain.C:788)
==7590==
==7590== LEAK SUMMARY:
==7590== definitely lost: 10,192 bytes in 26 blocks
==7590== indirectly lost: 224,224 bytes in 572 blocks
==7590== possibly lost: 6,512 bytes in 11 blocks
==7590== still reachable: 127,271,869 bytes in 13,183 blocks
==7590== of which reachable via heuristic:
==7590== stdstring : 89 bytes in 3 blocks
==7590== newarray : 9,993,560 bytes in 3 blocks
==7590== suppressed: 0 bytes in 0 blocks
通过分析代码,发现确实有内存泄漏,new的对象没有在析构时删除。
经过修改后,再次测试。直到 definitely lost: 0 bytes in 0 blocks为止。