IBM Rational Purify 使用记录1-找不到memory leak之解决方案

今天需要用IBM Rational Purify查找Memory Leaking, 于是开始研究,问题马上遇到了。

我先写了个测试的小程序,却发现运行有问题

——————————————————————

int   main() 

{      

char   *p   =   new char[16];      

return   0; 

---------------------------------------

在VS 2003 .net 中编译,如果是debug模式编译,用purify检查生成的exe,不会报错。如果用release模式编译,则会报错,错误信息如下:

[I] Starting Purify'd 

C:/Documents and Settings/Administrator.EMPIRIX-7A1AC0B/My Documents/Visual Studio Projects/2/Release/2.exe at 2008-05-16 10:52:47

[I] Starting main

[W] Summary of all memory leaks... {16 bytes, 1 block}

    [W] MLK: Memory leak of 16 bytes from 1 block allocated in heap_alloc [2.exe]

            Distribution of leaked blocks             Allocation location                 HeapAlloc     

 [C:/WINDOWS/system32/KERNEL32.dll]                 

heap_alloc     [f:/vs70builds/3077/vc/crtbld/crt/src/malloc.c:211]

                _safe_se_handler_count 

[C:/Documents and Settings/Administrator.EMPIRIX-7A1AC0B/My Documents/Visual Studio Projects/2/Release/2.exe]

[I] Exiting with code 0 (0x00000000)

[I] Program terminated at 2008-05-16 10:52:49

我的问题是:

1.为何debug模式下,无错误报出?

2.为何release模式下,报错却定位不到我的源程序?

我用的Purify是7.0的版本。 

找了一圈也没有找到解决办法,后来在IBM Forum里找到了答案,原来是VS 2003的编译开发环境问题。(也许其他同学在使用时候的default开发环境就满足了要求,所以很多介绍purify的文章都没有提到要更改编译开发环境的配置,而我却是unlucky,花了一天时间才解决掉。)

原文出处:

http://www-128.ibm.com/developerworks/forums/thread.jspa?threadID=200784&tstart=15

根据在线帮助里的推荐配置改一下即可,怀疑最可能出错的是(/fixed:no),但是也不确定。

 Recommended Settings for Visual Studio .NET and Visual Studio 2005 Related Topics For managed code If you intend the data you collect to include line numbers, debug data must be available to your Rational Runtime Analysis tool. To provide debug data, select Debug configuration in the Visual Studio Configuration Manager. Alternatively, compile with the /debug switch when you compile your program. Consult your compiler manual for information about the location of debug data. For languages such as C#, managed C++, and managed Visual Basic, the debug data is present in .pdb files. For native-compiled (unmanaged) code Here are the settings for Visual Studio .NET and Visual Studio 2005 that work best with Rational Runtime Analysis tools for native-compiled (unmanaged) code.

1.    In the Property Pages for your project, C/C++ folder, General page: Select Program Database in the Debug Information Format property drop-down list box.

2.    In the C/C++ folder, Code Generation page: Select Default in the Basic Runtime Checks property drop-down list box. This option setting disables the VC7 compiler's stack frame checking.

3.    In the Linker folder, General page: Select No in the Enable Incremental Linking property drop-down list box.

4.    In the Linker folder, Command Line page: In the Additional Options box add: /fixed:no. This adds relocation information to your image file.  

5.    In the C/C++ folder, Optimization page: ?        For Visual Studio 2005, set Whole Program Optimization to No.

6.    In the Linker folder, Input page: ?    Clear Delay Loaded Dlls. If you must use this setting then you need to exclude the module that is built with this setting from instrumentation.

After changing your Visual Studio settings, you should always: ?      Rebuild your application. ?      Verify that the .pdb file for the .exe or .dll is in the same directory as the corresponding .exe or .dll. The .pdb file contains the Microsoft format symbol information. If you move your .exe or .dll to a working directory, you must also move the corresponding .pdb file. ?      Clear your product's cache directory before running again. This forces reinstrumentation of the newly updated files. Notes: ?  A .dll from the Microsoft Debugger Interface Access SDK is used to collect debug data from pdb files generated by Visual Studio 2005. This is installed by default with Visual Studio 2005. It抯 a redistributable package, and the redistributable is installed as  <install dir>/Microsoft Visual Studio 8/SDK/v2.0/Bootstrapper/Packages/vcredist_x86.exe. You may need to run this to install DIA if you want to run your program on a different machine than that where Visual Studio 2005 is installed. (C) Copyright IBM Corporation 1992, 2006.  

更改之后,输出如下。

[I] Starting Purify'd C:/Documents and Settings/Administrator.EMPIRIX-7A1AC0B/My Documents/Visual Studio Projects/2/Debug/2.exe at 2008-05-16 16:27:37

[I] Starting main

[W] Summary of all memory leaks... {10 bytes, 1 block}   

[W] MLK: Memory leak of 10 bytes from 1 block allocated in main [2.exe]                Distribution of leaked blocks           

Allocation location               

malloc         [f:/vs70builds/3077/vc/crtbld/crt/src/dbgheap.c:138]                main           [c:/documents and settings/administrator.empirix-7a1ac0b/my documents/visual studio projects/2/2.cpp:5]                   

 int   main()                   

{                

=>     char   *p   = (char *)  malloc(10);                       

 return   0;                  

  }               

mainCRTStartup [f:/vs70builds/3077/vc/crtbld/crt/src/crt0.c:259]

[I] Exiting with code 0 (0x00000000)

[I] Program terminated at 2008-05-16 16:27:39

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值