<legend id='qjEw9'><style id='qjEw9'><dir id='qjEw9'><q id='qjEw9'></q></dir></style></legend>
  • <i id='qjEw9'><tr id='qjEw9'><dt id='qjEw9'><q id='qjEw9'><span id='qjEw9'><b id='qjEw9'><form id='qjEw9'><ins id='qjEw9'></ins><ul id='qjEw9'></ul><sub id='qjEw9'></sub></form><legend id='qjEw9'></legend><bdo id='qjEw9'><pre id='qjEw9'><center id='qjEw9'></center></pre></bdo></b><th id='qjEw9'></th></span></q></dt></tr></i><div id='qjEw9'><tfoot id='qjEw9'></tfoot><dl id='qjEw9'><fieldset id='qjEw9'></fieldset></dl></div>

        <bdo id='qjEw9'></bdo><ul id='qjEw9'></ul>

      1. <tfoot id='qjEw9'></tfoot>

        <small id='qjEw9'></small><noframes id='qjEw9'>

        使用 STL 容器的 C++ 内存泄漏

        时间:2023-09-26

          <i id='11bIE'><tr id='11bIE'><dt id='11bIE'><q id='11bIE'><span id='11bIE'><b id='11bIE'><form id='11bIE'><ins id='11bIE'></ins><ul id='11bIE'></ul><sub id='11bIE'></sub></form><legend id='11bIE'></legend><bdo id='11bIE'><pre id='11bIE'><center id='11bIE'></center></pre></bdo></b><th id='11bIE'></th></span></q></dt></tr></i><div id='11bIE'><tfoot id='11bIE'></tfoot><dl id='11bIE'><fieldset id='11bIE'></fieldset></dl></div>
          <legend id='11bIE'><style id='11bIE'><dir id='11bIE'><q id='11bIE'></q></dir></style></legend>
          • <bdo id='11bIE'></bdo><ul id='11bIE'></ul>

              <small id='11bIE'></small><noframes id='11bIE'>

                <tfoot id='11bIE'></tfoot>
                  <tbody id='11bIE'></tbody>
                1. 本文介绍了使用 STL 容器的 C++ 内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  以下代码导致内存泄漏(使用 Visual Studio):

                  The following code is giving me a memory leak (using Visual Studio):

                  #define _CRTDBG_MAP_ALLOC
                  #include <stdlib.h>
                  #include <crtdbg.h>
                  #include <vector>
                  #include <memory>
                  
                  struct Listener {};
                  
                  struct Subject
                  {
                      std::vector<Listener*> listeners;
                  };
                  
                  int main(void)
                  {
                      Subject subject;
                      _CrtDumpMemoryLeaks();
                      return 1;
                  }
                  

                  我认为这是因为在实例化 Subject 类时 STL 向量容器正在使用堆上的内存.如何确保程序退出时vector容器被销毁?(我已经尝试删除 Subject 析构函数中的容器,但这似乎不起作用).

                  I presume this is because the STL vector container is using memory on the heap when the Subject class is instantiated. How do I ensure that the vector container is destroyed when the program exits? (I've tried deleting the container in the Subject destructor, but that doesn't seem to work).

                  推荐答案

                  vector 在程序退出时被销毁,你不需要确保它.如果您不想将分配的内存报告为泄漏",您确实需要确保在销毁后调用 _CrtDumpMemoryLeaks:

                  The vector is destroyed when the program exits, you don't need to ensure it. You do need to ensure that _CrtDumpMemoryLeaks is called after that destruction if you don't want it to report the allocated memory as "leaked":

                  int main()
                  {
                      { Subject subject; }
                      _CrtDumpMemoryLeaks();
                      return 1;
                  }
                  

                  这篇关于使用 STL 容器的 C++ 内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:新建然后删除后的内存消耗 下一篇:GCC 内存泄漏检测相当于 Microsoft crtdbg.h?

                  相关文章

                  <legend id='1vba6'><style id='1vba6'><dir id='1vba6'><q id='1vba6'></q></dir></style></legend>
                  1. <small id='1vba6'></small><noframes id='1vba6'>

                  2. <tfoot id='1vba6'></tfoot>
                  3. <i id='1vba6'><tr id='1vba6'><dt id='1vba6'><q id='1vba6'><span id='1vba6'><b id='1vba6'><form id='1vba6'><ins id='1vba6'></ins><ul id='1vba6'></ul><sub id='1vba6'></sub></form><legend id='1vba6'></legend><bdo id='1vba6'><pre id='1vba6'><center id='1vba6'></center></pre></bdo></b><th id='1vba6'></th></span></q></dt></tr></i><div id='1vba6'><tfoot id='1vba6'></tfoot><dl id='1vba6'><fieldset id='1vba6'></fieldset></dl></div>
                    • <bdo id='1vba6'></bdo><ul id='1vba6'></ul>