zoukankan      html  css  js  c++  java
  • 静态链接到CRT(LIBCMT.lib)的隐患

    转自:http://www.codeproject.com/Articles/22642/What-Every-Computer-Programmer-Should-Know-About-W

    When the CRT/C++ Library is linked statically, then all its code is embedded into the resulting executable image. The problem is that internal CRT objects cannot be shared with other CRT instances. The memory allocated in one instance of CRT must be freed in the same instance, the file opened on one instance of CRT must be operated and closed by functions from the same instance, etc. It happens because the CRT tracks the acquired resources internally. Any attempt to free a memory chunk or read from a file via FILE* that came from another CRT instance will lead to corruption of the internal CRT state and most likely to crash.

    That's why linking CRT statically obligates a developer of a module to provide additional functions to release allocated resources and a user of a module to remember to call these functions in order to prevent resource leaks. No STL containers or C++ objects that use allocations internally can be shared across modules that link to the CRT statically. The following diagram illustrates the usage of a memory buffer allocated via a call to malloc.

    Diagram #2: Using memory allocated by malloc from different modules

    Diagram #2: Using memory allocated by malloc' from different modules

    In the above diagram, Module 1 is linked to the CRT statically(LIBCMT.lib), while Modules 2 and 3 are linked to the CRT dynamically(MSVCRT.dll). Modules 2 and 3 can pass CRT owned objects between them freely. For example, a memory chunk allocated with malloc in Module 3 can be freed in Module 2 with free. It is because both malloc and free calls will end up in the same instance of CRT.

    On the other hand, Module 1 cannot let other modules to free its resources. Everything allocated in Module 1 must be freed by Module 1. It is because only Module 1 has access to the statically linked instance of the CRT. In the above sample, Module 2 must remember to call a function from Module 1 in order to properly release the acquired memory.

  • 相关阅读:
    LeetCode:Validate Binary Search Tree
    二叉树的非递归遍历(非递归使用栈、非递归不使用栈)
    scala 基础语法
    scala VS python2 (linux or shell)
    web压力测试工具
    Elasticsearch cluster health: yellow unassigned shards
    GC overhead limit exceeded
    linux定时任务的设置
    linux CPU占用率高(转)
    JQuery Sparkline 说明文档
  • 原文地址:https://www.cnblogs.com/qinfengxiaoyue/p/2907664.html
Copyright © 2011-2022 走看看