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

      <tfoot id='HUbXg'></tfoot>
    1. <small id='HUbXg'></small><noframes id='HUbXg'>

    2. <legend id='HUbXg'><style id='HUbXg'><dir id='HUbXg'><q id='HUbXg'></q></dir></style></legend>
    3. C++ - 使用 _CrtDumpMemoryLeaks() 进行内存泄漏测试 - 不输出行号

      C++ - Memory leak testing with _CrtDumpMemoryLeaks() - Does not output line numbers(C++ - 使用 _CrtDumpMemoryLeaks() 进行内存泄漏测试 - 不输出行号)
      <tfoot id='9JOME'></tfoot>
    4. <legend id='9JOME'><style id='9JOME'><dir id='9JOME'><q id='9JOME'></q></dir></style></legend>
        <tbody id='9JOME'></tbody>

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

        <small id='9JOME'></small><noframes id='9JOME'>

                <bdo id='9JOME'></bdo><ul id='9JOME'></ul>
              • 本文介绍了C++ - 使用 _CrtDumpMemoryLeaks() 进行内存泄漏测试 - 不输出行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在 Visual Studio 2010 中使用 SDL 开发一款游戏.我遇到了 _CrtDumpMemoryLeaks() 宏,并想试一试.调用 _CrtDumpMemoryLeaks() 确实将内存泄漏打印到输出窗口,但它没有显示发生的位置.

                我在 阅读了 MSDN 文章内存泄漏检测启用 ,它解释了如果我定义 _CRTDBG_MAP_ALLOC 它应该输出违规语句的行号.在我的情况下不会发生这种情况.(但是,如果我直接使用 malloc() —— 而不是使用new",我能够让它工作).

                代码:

                #define _CRTDBG_MAP_ALLOC#include #include int main(int argc, char *argv[]) {int *var = new int(5);_CrtDumpMemoryLeaks();返回0;}

                输出如下:<代码><预>检测到内存泄漏!转储对象 ->{58} 位于 0x007D1510 的普通块,4 字节长.数据:<> 05 00 00 00对象转储完成.

                如果 _CrtDumpMemoryLeaks() 在使用new"分配时无法输出行号,那么我们感谢提供其他实现类似行为的方法的建议.

                解决方案

                当您定义 _DEBUG 并包含 <crtdbg.h> 时,您会得到一个重载的 operator new接受额外的参数,您可以使用这些参数来指定放置 new 表达式中的文件和行号.

                例如

                int* p = new (_NORMAL_BLOCK, __FILE__, __LINE__) int(5);

                您可以将其包装在条件定义的宏中,例如

                #ifdef _DEBUG#define DEBUG_NEW_PLACEMENT (_NORMAL_BLOCK, __FILE__, __LINE__)#别的#define DEBUG_NEW_PLACEMENT#万一int* p = new DEBUG_NEW_PLACEMENT int(5);

                虽然您确实看到人们定义了一个宏 new 来完全隐藏此表单客户端代码,但我个人并不推荐它,因为它破坏了任何已经有意使用 Placement new 的内容,您必须确保任何在重新定义 new 的任何标头之前包含使用放置 new 的标头(例如许多标准标头).这可以很容易地让头文件中 new 的一些内联使用不经调整".

                I'm working on a game with SDL in Visual Studio 2010. I came across the _CrtDumpMemoryLeaks() macro and thought I'd give it a go. Invoking _CrtDumpMemoryLeaks() does print memory leaks to the output window, but it does not show where it happens.

                I've read the MSDN article at Memory Leak Detection Enabling , and it explains that if I define _CRTDBG_MAP_ALLOC it should output the line number of the offending statement. This does not happen in my case. (I was however able to get it to work if I use malloc() directly -- not by using 'new').

                The code:

                #define _CRTDBG_MAP_ALLOC
                #include <crtdbg.h>
                #include <stdlib.h>
                
                int main(int argc, char *argv[]) {
                    int *var = new int(5);
                
                    _CrtDumpMemoryLeaks();
                
                    return 0;
                }
                

                The output is the following:

                Detected memory leaks!
                Dumping objects ->
                {58} normal block at 0x007D1510, 4 bytes long.
                 Data: <    > 05 00 00 00 
                Object dump complete.
                

                If _CrtDumpMemoryLeaks() is unable to output line numbers when allocating using 'new' then suggestions for other ways to achieve similar behavior is appreciated.

                解决方案

                When you define _DEBUG and include <crtdbg.h> you get an overloaded operator new which takes additional parameters which you can use to specify the file and line numbers in placement new expressions.

                E.g.

                int* p = new (_NORMAL_BLOCK, __FILE__, __LINE__) int(5);
                

                You can wrap this in a conditionally defined macro, e.g.

                #ifdef _DEBUG
                #define DEBUG_NEW_PLACEMENT (_NORMAL_BLOCK, __FILE__, __LINE__)
                #else
                #define DEBUG_NEW_PLACEMENT
                #endif
                
                int* p = new DEBUG_NEW_PLACEMENT int(5);
                

                While you do see people defining a macro new to completely hide this form client code, I do not personally recommend it as it breaks anything already intentionally using placement new and you have to make sure that any headers using placement new (such as many standard headers) are included before any header redefining new. This can make it easy to let some inline uses of new in header files slip through without being 'adjusted'.

                这篇关于C++ - 使用 _CrtDumpMemoryLeaks() 进行内存泄漏测试 - 不输出行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)

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

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

                      <tbody id='sd93y'></tbody>
                      <bdo id='sd93y'></bdo><ul id='sd93y'></ul>
                    • <legend id='sd93y'><style id='sd93y'><dir id='sd93y'><q id='sd93y'></q></dir></style></legend>
                          <tfoot id='sd93y'></tfoot>