首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >_CRTDBG_MAP_ALLOC不显示文件名和行号

问_CRTDBG_MAP_ALLOC不显示文件名和行号
EN

Stack Overflow用户
提问于 2020-08-31 20:25:03
回答 1查看 308关注 0票数 0

我使用Visual Studio 2017。我使用一个动态库来转储内存泄漏:_CrtDumpMemoryLeaks();我添加了一个适当的宏定义:

代码语言:javascript
复制
#define _CRTDBG_MAP_ALLOC

但是输出仍然不包含文件名和行号。当我调试该dll时,我可以单步执行以下malloc定义:

代码语言:javascript
复制
//
// malloc.cpp
//
//      Copyright (c) Microsoft Corporation. All rights reserved.
//
// Implementation of malloc().
//
#include <corecrt_internal.h>
#include <malloc.h>



// Allocates a block of memory of size 'size' bytes in the heap.  If allocation
// fails, nullptr is returned.
//
// This function supports patching and therefore must be marked noinline.
// Both _malloc_dbg and _malloc_base must also be marked noinline
// to prevent identical COMDAT folding from substituting calls to malloc
// with either other function or vice versa.
extern "C" _CRT_HYBRIDPATCHABLE __declspec(noinline) _CRTRESTRICT void* __cdecl malloc(size_t const size)
{
    #ifdef _DEBUG
    return _malloc_dbg(size, _NORMAL_BLOCK, nullptr, 0);
    #else
    return _malloc_base(size);
    #endif
}

另一方面,当我在Visual Studio中创建一个简单的控制台应用程序时,我不能进入malloc,但是内存泄漏输出会显示文件名和行号。怎么回事?如何解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-01 04:53:00

文档Find memory leaks with the CRT library中描述的技术。

关键点在于,您需要在任何#include之前添加以下3行

代码语言:javascript
复制
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63670784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档