<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() 进行内存泄漏测试 - 不输出行号

      时间:2023-09-26
      <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() 进行内存泄漏测试 - 不输出行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:什么是 C/C++ 程序及其插件 DLL 的最佳空闲内存泄漏检测器? 下一篇:CString 到 char*

                相关文章

                <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'>

                    <bdo id='sd93y'></bdo><ul id='sd93y'></ul>
                1. <legend id='sd93y'><style id='sd93y'><dir id='sd93y'><q id='sd93y'></q></dir></style></legend>
                    <tfoot id='sd93y'></tfoot>