如何解决_CRTDBG_MAP_ALLOC未显示文件名和行号
我使用Visual Studio2017。我使用一个动态库来转储内存泄漏:_CrtDumpMemoryLeaks();
我添加了一个适当的宏定义:
#define _CRTDBG_MAP_ALLOC
,但是输出仍然不包含文件名和行号。当我调试该dll时,我可以进入 以下malloc定义:
//
// 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,但是内存泄漏输出显示了文件名和行号。这是怎么回事 ?如何解决?
解决方法
文档Find memory leaks with the CRT library中描述的技术。
关键点在于,您需要在任何#include
之前添加以下3行:
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。