在一个非常简单的程序中检测到内存泄漏.怎么办
Memory leak detected in a very simple program. What to do?
我的大程序中有一个内存泄漏,被Visual Studio CRT调试系统检测到。我将程序缩减为以下内容,仍然显示内存泄漏。
#include "stdafx.h"
#include "crtdbg.h"
int main()
{
int tmp = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
_CrtSetDbgFlag(tmp | _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
int* k = new int(8);
delete k;
return 0;
}
当我在Visual Studio 2012系统中运行它时,我看到以下内容:
Detected memory leaks!
Dumping objects ->
{65} normal block at 0x00663008, 4424 bytes long.
Data: <X #f > 58 CF 14 00 90 23 66 00 01 00 00 00 00 00 00 00
{64} normal block at 0x00662390, 4 bytes long.
Data: < > 00 C3 14 00
Object dump complete.
如果我删除分配和解除分配,则不会出现泄漏。如果我用任何使用内存分配的标准库功能替换分配和解除分配(例如 std::string k
),泄漏出现。
为什么会出现内存泄漏?如何删除它们?
我尝试通过将_crtBreakAlloc
设置为 64 来调试我的问题;系统停在应该帮助我的地方(请参阅下面的堆栈跟踪)。但我不知道如何处理这些信息。
> test_it.exe!_heap_alloc_dbg_impl(unsigned int nSize, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) Line 393 C++
test_it.exe!_nh_malloc_dbg_impl(unsigned int nSize, int nhFlag, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) Line 239 C++
test_it.exe!_nh_malloc_dbg(unsigned int nSize, int nhFlag, int nBlockUse, const char * szFileName, int nLine) Line 302 C++
test_it.exe!malloc(unsigned int nSize) Line 56 C++
test_it.exe!_PlatformSpecificMalloc() Unknown
test_it.exe!MemoryLeakWarningPlugin::ignoreAllLeaksInTest(void) Unknown
test_it.exe!operator new(unsigned int) Unknown
test_it.exe!MemoryLeakWarningPlugin::getGlobalDetector(void) Unknown
test_it.exe!std::error_condition::value(void) Unknown
test_it.exe!operator new(unsigned int) Unknown
test_it.exe!main() Line 9 C++
我的系统是:
- Microsoft Visual Studio Professional 2012
- 版本 11.0.61030.00 更新 4
- 视觉C++ 2012 04938-004-0034007-02224
- 视窗 7
调用堆栈表明除了 Visual C++ 运行时函数之外,还使用了另一个内存泄漏工具。
使用谷歌将我带到这个链接:https://github.com/auser/cpputest/blob/master/src/CppUTest/MemoryLeakWarningPlugin.cpp
因此,cppuTest
可能正在应用于您的简单项目,而您却没有意识到它。
我建议你创建一个全新的 Win32 控制台应用程序,复制并粘贴你的代码,然后重新测试。 确保新项目没有其他依赖项。
刚刚尝试了干净的VS 2012安装并安装了Deleaker。未显示泄漏。并且决赛中没有 CRT 的输出。
堆栈跟踪中提到的 MemoryLeakWarningPlugin 是什么?似乎它是CppUTest的一部分(我做了一点谷歌)。
我认为要么MemoryLeakWarningPlugin泄漏自己,要么以某种方式破坏CRT诊断系统。
相关文章:
- CoreCLR 中的检测探查器 - 将帮助程序程序集加载到 dotnet 进程的方法
- 程序停止运行,但未检测到错误
- JNI 在应用程序中检测到错误:在为 innerclass 调用 NewObject 时使用了无效的 jobject
- 捕获全部处理程序检测C++中的子类异常
- Android (ART) 崩溃并显示错误 JNI 在应用程序中检测到错误:jstring 是无效的本地引用
- Qt安装程序框架:检测是否安装了VC++可再发行组件
- 如何将此项目更改为 OpenCV 实时人脸检测应用程序?
- 如何让我的C++程序检测 -
- 即使我已经安装了 poco,应用程序也无法检测到C++ poco
- 排序程序的错误(检测到堆损坏)
- 如果在C 程序中使用OpenMP,请在运行时检测
- CRT 检测到应用程序在堆缓冲区(新建/删除)类结束后写入内存
- Windows Gflags应用程序堆检测堆
- OpenCV程序的输出在未检测到文本对象时不给出0
- 如何在 clang 消毒器发现错误时终止 clang 检测程序
- 如何检测程序运行时 Kinect 何时断开连接/拔出
- 如果我在 Windows 中编程我的深度数据包检测程序,我会错过什么主要的事情
- Visual C++-检测程序是否已加载到Visual Debugger中
- Linux命令,用于检测程序的计算机资源使用情况
- 如何检测c/c++程序中可能/潜在的堆栈溢出问题