Intel 编译器 内存安全检查 真心的很详细 转

Managing Memory Errors with Intel? Parallel Inspector XE


___________________________________________________________________

Intel? Academic Community

Disclaimer

The information contained in this document is provided for informational
purposes only and represents the current view of Intel Corporation ("Intel") and
its contributors ("Contributors") on, as of the date of publication. Intel and
the Contributors make no commitment to update the information contained in this
document, and Intel reserves the right to make changes at any time, without
notice.

DISCLAIMER. THIS DOCUMENT, IS PROVIDED "AS IS." NEITHER INTEL, NOR THE
CONTRIBUTORS MAKE ANY REPRESENTATIONS OF ANY KIND WITH RESPECT TO PRODUCTS
REFERENCED HEREIN, WHETHER SUCH PRODUCTS ARE THOSE OF INTEL, THE CONTRIBUTORS,
OR THIRD PARTIES. INTEL, AND ITS CONTRIBUTORS EXPRESSLY DISCLAIM ANY AND ALL
WARRANTIES, IMPLIED OR EXPRESS, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR ANY PARTICULAR PURPOSE, NON-INFRINGEMENT, AND ANY
WARRANTY ARISING OUT OF THE INFORMATION CONTAINED HEREIN, INCLUDING WITHOUT
LIMITATION, ANY PRODUCTS, SPECIFICATIONS, OR OTHER MATERIALS REFERENCED HEREIN.
INTEL, AND ITS CONTRIBUTORS DO NOT WARRANT THAT THIS DOCUMENT IS FREE FROM
ERRORS, OR THAT ANY PRODUCTS OR OTHER TECHNOLOGY DEVELOPED IN CONFORMANCE WITH
THIS DOCUMENT WILL PERFORM IN THE INTENDED MANNER, OR WILL BE FREE FROM
INFRINGEMENT OF THIRD PARTY PROPRIETARY RIGHTS, AND INTEL, AND ITS CONTRIBUTORS
DISCLAIM ALL LIABILITY THEREFOR. INTEL, AND ITS CONTRIBUTORS DO NOT WARRANT THAT
ANY PRODUCT REFERENCED HEREIN OR ANY PRODUCT OR TECHNOLOGY DEVELOPED IN RELIANCE
UPON THIS DOCUMENT, IN WHOLE OR IN PART, WILL BE SUFFICIENT, ACCURATE, RELIABLE,
COMPLETE, FREE FROM DEFECTS OR SAFE FOR ITS INTENDED PURPOSE, AND HEREBY
DISCLAIM ALL LIABILITIES THEREFOR. ANY PERSON MAKING, USING OR SELLING SUCH
PRODUCT OR TECHNOLOGY DOES SO AT HIS OR HER OWN RISK.

Licenses may be
required. Intel, its contributors and others may have patents or pending patent
applications, trademarks, copyrights or other intellectual proprietary rights
covering subject matter contained or described in this document. No license,
express, implied, by estoppels or otherwise, to any intellectual property rights
of Intel or any other party is granted herein. It is your responsibility to seek
licenses for such intellectual property rights from Intel and others where
appropriate. Limited License Grant. Intel hereby grants you a limited copyright
license to copy this document for your use and internal distribution only. You
may not distribute this document externally, in whole or in part, to any other
person or entity. LIMITED LIABILITY. IN NO EVENT SHALL INTEL, OR ITS
CONTRIBUTORS HAVE ANY LIABILITY TO YOU OR TO ANY OTHER THIRD PARTY, FOR ANY LOST
PROFITS, LOST DATA, LOSS OF USE OR COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES, OR FOR ANY DIRECT, INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF YOUR USE OF THIS DOCUMENT OR RELIANCE UPON THE INFORMATION CONTAINED
HEREIN, UNDER ANY CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF
WHETHER INTEL, OR ANY CONTRIBUTOR HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH
DAMAGES. THESE LIMITATIONS SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE
ESSENTIAL PURPOSE OF ANY LIMITED REMEDY.

Intel and Intel logo are trademarks or registered trademarks of Intel
Corporation or its subsidiaries in the United States and other countries.

*Other names and brands may be claimed as the property of others.

Copyright
? 2010, Intel Corporation. All Rights Reserved.Table of Contents

Disclaimer    ii

Parallelism Tools –
Checking for Memory Access
Violations    1

Activity 1 - Choose, Build and Run
a project    1

Activity 2 - Configure and Run
Analysis    8

Activity 3 - Manage result data
after analysis    12

Activity 4 - Resolve, Rebuild and
Rerun    17

Parallelism Tools – Checking for Memory Access
violations

Activity 1 – Choose, Build and Run a Project








Time
Required

Ten
minutes

Objective

  • Learn how to
    open and build a project in Microsoft Visual
    Studio*

The
application is a 2-D ray tracer/renderer called tachyon written in C++.
The code is small enough that you may be able to identify potential memory
access violations by visual inspection. If you identify and make a list of
problems, check your list with the list that Parallel Inspector XE identifies.

Choose a project


  1. With
    Windows Explorer*, locate the samples folder in the Parallel
    Inspector XE directory.

  2. From the
    Microsoft Visual Studio* menu, choose File > Open >
    Project/Solution
    .

  3. In the Open Project dialog box, open
    the filesamples\windows\tachyon_insp_xe\vc8\tachyon_insp_xe.sln fileby double-clicking it.

    This
    should now display tachyon_insp_xe solution in the
    Solution Explorer.

  1. In the
    Solution Explorer, right-click the
    find_and_fix_memory_errors project and choose Set as
    Startup Project
    from among the menu options.

Build a project


  1. We first
    need to verify debug mode is configured to produce the best results. So in the
    Solution Explorer, right-click the
    find_and_fix_memory_errors project and choose
    Properties to display the Property Pages
    dialog box.

  2. Verify
    the Configuration drop-down list is set to
    Debug or Active(Debug).

  1. In the left pane, choose Configuration
    Properties > C/C++ > General
    .

  2. Verify the Debug Information
    Format
    is set to Program Database (/Zi) or
    Program Database for Edit & Continue (/ZI).


  3. In the left pane, choose Configuration
    Properties > C/C++ > Optimization
    .

  4. Verify the Optimization
    field is set to Disabled (/Od).


  5. In the left pane, choose Configuration
    Properties > C/C++ > Code Generation
    .

  6. Verify the Runtime Library
    field is set to Multi-threaded DLL (/MD) or
    Multi-threaded Debug DLL (/MDd).


  7. In the
    left pane, choose Configuration Properties > Linker >
    Debugging
    .

  8. Verify the Generate Debug
    Info
    field is set to Yes (/DEBUG).


  9. Verify
    the project is set to build in debug mode. Click the Configuration
    Manager
    button and verify the Active solution
    configuration
    drop-down list is set to Debug.


  10. Click the Close button to
    close the Configuration Manager dialog box.

  11. Click the OK button to close
    the Property Pages dialog box.

Run a project


  1. From the Visual Studio* menu, choose
    Debug > Start Without Debugging.

  2. When the Visual Studio* IDE responds this
    project is out of date, click Yes to build the project.

  3. The application is run but the application
    output window will be empty and grey because of memory errors in the code!

  4. You verified the find_and_fix_memory_errors
    project is set to produce the most accurate, complete results, compiled and
    linked the project and ensured the resulting find_and_fix_memory_errors.exe
    file runs on your system outside the Inspector XE.

Activity 2 – Configure and Run analysis








Time
Required

Ten
minutes

Objective

  • Learn how to
    choose and run a memory error analysis to detect memory errors that may
    need handling.

Configure a memory error
analysis

  1. From the Visual Studio* menu, choose
    Tools > Intel Inspector XE 2011 > New Analysis to
    display an Analysis Type window similar to the following:


  2. Open the Memory Error
    Analysis
    folder and choose the Detect Memory
    Problems
    analysis type to display a window similar to the following.
    (If necessary, click the icon
    in the Details region to display analysis type
    characteristics.)

















Use the Navigation toolbar
to navigate among Inspector XE windows. The buttons on the toolbar vary
depending on the displayed window.

The Analysis Types region
shows available preset analysis types. You can also use existing analysis
types as templates to create custom analysis types.

Use the checkbox(es) and drop-down field(s)
to fine-tune some, but not all, analysis type characteristics. If you need
to fine-tune more analysis type characteristics, choose another preset
analysis type or create a custom analysis type.

The Details region shows
all current analysis type characteristics. Try choosing a different preset
analysis type or checkbox/drop-down list value to see the impact on the
Details region.

Use the Command toolbar to
control analysis runs and perform other functions. For example, use the
Project Properties button to redisplay the
Project Properties dialog box.

  1. After you finish experimenting, ensure your
    Analysis Type window resembles the following window:


  2. You
    chose a preset analysis type of intermediate scope to detect memory errors in
    the find_and_fix_memory_errors.exe file.

Run
a memory error analysis

  1. Click
    the Analyze button on the Analysis Type
    window to:

  • Execute
    the find_and_fix_memory_errors.exe file.

  • Identify
    memory errors that may need handling.

  • Collect
    the result in a folder in the tachyon_insp_xe\vc8\My Inspector XE 2011
    Project - find_and_fix_memory_errors folder.

  • Finalize
    the result (convert symbol information into filenames and line numbers,
    perform duplicate elimination, and form problem sets).

  1. During
    collection, the Inspector XE displays a Collection Log window
    similar to the following:














The Visual Studio* IDE offers a pointer to
the result from the Solution Explorer to provide easy
future access. Here, the Visual Studio* IDE created a
MyInspector XE
Results/find_and_fix_memory_errors
folder in the Solution
Explorer
. Open it to see the pointer to the
r000mi2 result.

The result name appears in the tab. Here,
the name of the result (and the name of the result folder in the tachyon_insp_xe\vc8\My Inspector XE 2011
Project - find_and_fix_memory_errors folder) is r000mi2,
where

  • r = constant,

  • 000 = next available number,

  • mi = memory error analysis type,

  • 2 = preset analysis type of intermediate
    scope

The Collection Log pane
shows detected problems in real time.

This tutorial does not cover examining and
solving problems during collection. For more details, please check the
Inspector XE Help Index for examine result during analysis.

The Collector Messages
pane shows analysis status messages.

You can control where application output
appears: Separate console window (as in this tutorial),
Application Output pane in the Collection
Log
window, or Visual Studio* output
window.

Activity 3 – Manage result data after analysis








Time
Required

Fifteen
minutes

Objective

  • Learn how to
    manage result data after analysis is
    complete

After
analysis (both collection and finalization) completes successfully, the
Inspector XE displays the Summary window:

Choose
Problem Set

Start
exploring a memory error.

    Interpret
the Summary window

















The Summary window is the
starting point for managing result data. It groups observations into
problem sets, and then prioritizes the problem sets by severity and size.

Notice the arrow to the left of the
Analysis Type button. Use it to revisit a read-only
window showing your target project properties.

Think of the Problem Sets
pane as a to-do list. Start at the top and work your way
down.

The Observations in Problem
Set
pane shows all the observations in all the problems in the
selected problem set. By default, the Inspector XE selects the first
problem set for you.

Here, the Inspector XE selected the
Invalid Memory Access problem set, which contains one
problem composed of a Write observation.

This tutorial does not cover manipulating
the timeline. For more details, check the Inspector XE Help Index for
manipulate timeline.

The Summaries/Subsets pane
categorizes all displayed problem sets by specific criteria. Use it to
temporarily limit the list in the Problem Sets pane to
only those problem sets that meet specific criteria.

This tutorial does not cover searching for
specific problem sets. For more details, check the Inspector XE Help Index
for select/problem set search criteria.

Choose
a problem set

Double-click the data row for the
Mismatched allocation/deallocation problem set source file to
display the Sources window, which provides more visibility into
the error:

Interpret
Result Data

In
this section, we will see how to determine the cause of the detected memory
error.

To
interpret the Sources window:














Like the pane on the
Overview window, the Observations in Problem
Set
pane shows all the observations in the Mismatched
allocation/deallocation
problem in the Mismatched
allocation/deallocation
problem set.

The Allocation site
observation represents the location and associated call stack from which
the memory block was allocated. The Mismatched deallocation
site
observation represents the location and associated call
stack attempting the deallocation.

The Related Observation
Code
pane shows the source code in the find_and_fix_memory_errors.cpp
source file surrounding the Allocation site observation.
(Notice the icon
in the pane title matches the icon
on the Allocation site observation data row in the
Observations in Problem Set window.) The source code
corresponding to the Allocation site observation is
highlighted.

The Focus Observation Code
pane shows the source code in the find_and_fix_memory_errors.cpp source
file surrounding the Mismatched deallocation site
observation. (Notice the icon
in the pane title matches the icon
on the Mismatched deallocation site observation data row
in the Observations in Problem Set window.) The source
code corresponding to the Mismatched deallocation site
observation is highlighted.

This tutorial does not cover manipulating
the timeline. For more details, check the Inspector XE Help Index for
manipulate timeline.

To
interpret window icons:












Icon

Meaning

This observation is the focus observation.
The Inspector XE chose it for you when you double-clicked the
Mismatched allocation/deallocation problem set on the
Summary window. Its source code is currently displayed in
the Focus Observation Code pane.

Observation source code is available for
viewing in the Inspector XE and editing in the Visual Studio* editor.

This observation is related to the focus
observation. Its source code is currently displayed in the Related
Observation Code
pane.

To
understand the issue:

Look at the code in the Focus Observation
Code
pane and the Related Observation Code pane.

The code in the Allocation site
observation in the Related Observation Code pane contains a new
allocator, while the code in the Mismatched deallocation site
observation in the Focus Observation Code pane contains a free() deallocator.

A Mismatched
allocation/deallocation
problem occurs when you attempt a deallocation
with a function that is not the logical reflection of the allocator. In the C++
programming language, the following are matched reflections:

  • newanddelete

  • new[]anddelete[]

  • malloc()andfree()

Only the matching deallocation technique is
uniquely aware of all the memory allocation techniques and internal data storage
used by the allocation technique. Using the wrong deallocation technique will
almost certainly corrupt memory reclamation, its sole job.

Activity 4 – Resolve, Rebuild and Rerun








Time
Required

Twenty
minutes

Objective

  • Learn how to
    resolve a problem detected by the Inspector
    XE

Resolve
an issue:

  1. Double-click the
    highlighted code in the Focus Observation Code pane to open
    the find_and_fix_memory_errors.cpp source file in a separate tab where you can
    edit it with the Visual Studio* editor:

  1. Comment
    free(drawing);and uncomment//delete
    drawing;:

  1. Click
    the r000mi2 tab to redisplay the Sources
    window:

  1. Click
    the Summary button to redisplay the Summary
    window:

Resolve
another issue:

Now resolve another detected memory error by
following the below steps:

Chooseanother problem
set:

In the Problem Sets pane on the
Summary window, double-click the data row for a Memory
Leak
problem set to display the Sources window:

To access more information on
interpreting and resolving problems:

  1. Right-click the
    Allocation site observation in the Observations in
    Problem Set
    pane.

  2. Choose
    Explain Problem to display Inspector XE Help information for
    the Memory Leak problem type.

To interpret result data:

A Memory leak problem occurs
when a block of memory is allocated and never released.

The Allocation site observation
represents the location from which the memory block was allocated. Here, the
source code shows a malloc()
allocator.

Scroll the source code to near line 180:

The issue is obvious: The free()
call that releases the memory allocated by the malloc()
call is commented out.

To resolve memory leak problems (while avoiding
related mismatched allocation/deallocation problems): Match anymalloc()call
with afree()call,
anewwith adelete, and
anew[]with adelete[]as
soon as all use of the allocated space has occurred, but not sooner. Never mix
and match.

Be aware:

  • If you
    do not deallocate soon enough, you waste space the program might need.

  • If you
    deallocate too soon, you may do one of the following:

    • Corrupt your
      memory allocator or the space it manages when you later write to that space.

    • End up
      with bogus data if you later read that space when you previously returned it
      to the memory allocator.

  • Make
    sure you free any space allocated within a function prior to exiting that
    function, or else make sure the function caller does the deallocation for the
    called function.

To resolve the issue:

  1. Click
    the find_and_fix_memory_errors.cpp tab to redisplay the
    find_and_fix_memory_errors.cpp source file in the Visual Studio*
    editor:

  1. Uncomment//free(local_mbox);:

  1. Click
    the r000mi2 tab to redisplay the Sources
    window:

Rebuild
and Rerun Analysis

To see if
your edits resolved the memory errors:

  • Rebuild
    the project with your edited source code.

  • Run
    another analysis.

  • Start
    managing result data after collection.

To
rebuild the project
:

  1. Right-click the
    find_and_fix_memory_errors project in the Solution
    Explorer
    .

  2. Choose Build.

To
run another analysis
:

From the
Visual Studio* menu, choose Tools > Intel Inspector
XE 2011
> Analyze As... > Memory Error
Analysis / Detect Memory Problems
to run another analysis of the same
analysis type:

Notice the
image now displays.

To
start managing result data after collection
:

After
analysis (both collection and finalization) completes successfully, the
Inspector XE displays the Summary window:

Notice the
Inspector XE:

  • Created
    a new result tab.

  • No
    longer detected Mismatched allocation/deallocation and
    Memory leak problems.

Try fixing
the remaining problem set(s) yourself.

Intel 编译器 内存安全检查 真心的很详细 转,布布扣,bubuko.com

时间: 2024-11-03 22:00:57

Intel 编译器 内存安全检查 真心的很详细 转的相关文章

Intel 编译器 静态安全检查 真心的很详细 转

Static Security Analysis with Intel? Parallel Inspector XE ___________________________________________________________________ Intel? Academic Community Disclaimer The information contained in this document is provided for informational purposes only

Intel 编译器 线程安全检查 真心的很详细 转

Managing Threading Errors with Intel? Parallel Inspector XE ___________________________________________________________________ Intel? Academic Community Disclaimer The information contained in this document is provided for informational purposes onl

C++内存管理(超长,例子很详细,排版很好)

[导语] 内存管理是C++最令人切齿痛恨的问题,也是C++最有争议的问题,C++高手从中获得了更好的性能,更大的自由,C++菜鸟的收获则是一遍一遍的检查代码和对C++的痛恨,但内存管理在C++中无处不在,内存泄漏几乎在每个C++程序中都会发生,因此要想成为C++高手,内存管理一关是必须要过的,除非放弃C++,转到Java或者.NET,他们的内存管理基本是自动的,当然你也放弃了自由和对内存的支配权,还放弃了C++超绝的性能.本期专题将从内存管理.内存泄漏.内存回收这三个方面来探讨C++内存管理问题

Java多线程与并发---学习总结(很详细)

Java多线程与并发---学习总结(很详细) 1.      计算机系统 使用高速缓存来作为内存与处理器之间的缓冲,将运算需要用到的数据复制到缓存中,让计算能快速进行:当运算结束后再从缓存同步回内存之中,这样处理器就无需等待缓慢的内存读写了. 缓存一致性:多处理器系统中,因为共享同一主内存,当多个处理器的运算任务都设计到同一块内存区域时,将可能导致各自的缓存数据不一致的情况,则同步回主内存时需要遵循一些协议. 乱序执行优化:为了使得处理器内部的运算单位能尽量被充分利用. 2.      JAVA

很详细、很移动的Linux makefile教程:介绍,总述,书写规则,书写命令,使用变量,使用条件推断,使用函数,Make 的运行,隐含规则 使用make更新函数库文件 后序

很详细.很移动的Linux makefile 教程 内容如下: Makefile 介绍 Makefile 总述 书写规则 书写命令 使用变量 使用条件推断 使用函数 make 的运行 隐含规则 使用make更新函数库文件 后序 近期在学习Linux下的C编程,买了一本叫<Linux环境下的C编程指南>读到makefile就越看越迷糊,可能是我的理解能不行. 于是google到了以下这篇文章.通俗易懂.然后把它贴出来,方便学习. 后记,看完发现这篇文章和<Linux环境下的C编程指南>

转: angularjs学习总结(~~很详细的教程)

1 前言 前端技术的发展是如此之快,各种优秀技术.优秀框架的出现简直让人目不暇接,紧跟时代潮流,学习掌握新知识自然是不敢怠慢. AngularJS是google在维护,其在国外已经十分火热,可是国内的使用情况却有不小的差距,参考文献/网络文章也很匮乏.这里便将我学习AngularJS写成文档,一方面作为自己学习路程上的记录,另一方面也给有兴趣的同学一些参考. 首先我自己也是一名学习者,会以学习者的角度来整理我的行文思路,这里可能只是些探索,有理解或是技术上的错误还请大家指出:其次我特别喜欢编写小

内部类详解(很详细)

内部类详解(很详细) (转)内部类有两种情况: (1) 在类中定义一个类(私有内部类,静态内部类) (2) 在方法中定义一个类(局部内部类,匿名内部类) 1.私有内部类 -- 在方法之间定义的内部类,非静态 我们首先看看类中内部类的两个特点: (1) 在外部类的作用范围内可以任意创建内部类对象,即使内部类是私有的(私有内部类).即内部类对包围它的外部类可见. //代码1:内部类对外部类可见 class Outer{ //创建私有内部类对象 public Inner in=new Inner();

使用Intel编译器获得一致的浮点数值计算结果

大多数十进制的浮点数, 用二进制表示时不是完全一致的; 与此同时, 大多数与浮点数值相关的计算结果, 存在着固有的不确定性. 通常, 编写浮点计算应用软件希望达到如下的目标:  - 准确性:     意味着该产品产生的计算结果,应当"接近"于实际计算的结果; 评判的标准是误差值, 有时候也采用最后几位("units in the last place", ulp)  - 可复制性:    意味着该产品始终产生一致的结果, 无论运行的先后, 采用不同的编译选项, 使用

C++运算符重载总结(真的很详细了w(?Д?)w)

C++运算符重载总结(真的很详细了w(?Д?)w) 概述 运算符重载可以使得一些特殊类型参与运算,我是这样理解的. 使用友元形式的运算符重载 //一般形式 class x{ friend 返回类型 operator 运算符(形参表); }; 返回类型 operator 运算符(形参表) {} 双目运算符 所以加减乘除就可以这样来进行声明了 返回类型 operator +(-.*./) (形参表); 单目运算符 如- & ! ++(前),++(后)等.下面以++(后)来进行一个示例: class