使用Memory Allocation API检测用户自定义的内存泄漏

2014-11-24 09:19:51 · 作者: · 浏览: 0

我们知道,Intel Inspector XE 2011 有一套接口函数库,比如支持在用户程序中Pause/Resume 性能分析,自定义的同步变量的识别, 帧(Frame)级别的性能分析,等。本篇要介绍的是另外一种功能,如何侦测自定义的内存泄露。即用户从自己的内存池里申请内存,而又没有释放。


一般工作流程:找到相关的INCLUDE文件和库文件。


相关的Memory Allocation APIs



以下用一个简单的例子(final_test_heap_api.cpp),介绍如何使用以上的APIs


1. 编译源代码 (使用Intel C/C++ Compiler 12.0)


[root@kentsfield-01 peter]# source /opt/intel/compilerpro-12.0/bin/compilervars.sh intel64


[root@kentsfield-01 peter]# icpc -g final_test_heap_api.cpp libittnotify.a -o final_test_heap_api –lpthread


2. 使用Inspector XE 检测内存


[root@kentsfield-01 peter]# source /opt/intel/inspector_xe_2011/inspxe-vars.sh


Copyright (C) 2009-2011 Intel Corporation. All rights reserved.


Intel(R) Inspector XE 2011 (build 148563)


[root@kentsfield-01 peter]# inspxe-cl -collect mi3 -- ./final_test_heap_api


Used suppression file(s): []


my_malloc_heap_h is 0


my_free_heap_h is 1


my heap start addr = 0x19dd0f10


allocating of int_p at 0x19dd0f10


allocating of int_q at 0x19dd0f14


allocating of string at 0x19dd0f18


allocating of string at 0x19dd0f28


This is the test program


2 new problem(s) found


2 Memory leak problem(s) detected


3. 可以使用Inspector XE 图形界面,在源代码级浏览结果(Call Stack:Caller/Callee)