gpt4 book ai didi

c++ - 如何修复堆错误?

转载 作者:太空宇宙 更新时间:2023-11-04 01:38:12 24 4
gpt4 key购买 nike

我在 C++ 中运行一个迭代过程,在第一次迭代按预期完成后,我在第二次开始时收到以下错误:

Heap block at 00000000212005D0 modified at 0000000021200674 past requested size of 94 Windows has triggered a breakpoint in myProject.exe.

This may be due to a corruption of the heap, which indicates a bug in myProject.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while myProject.exe has focus.

The output window may have more diagnostic information.

在显示此错误时,visual-c++ 打开 dbgheap.c,突出显示给定的部分:

extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(
const void * pUserData
)
{
if (!pUserData)
return FALSE;

if (!_CrtIsValidPointer(pHdr(pUserData), sizeof(_CrtMemBlockHeader), FALSE))
return FALSE;

return HeapValidate( _crtheap, 0, pHdr(pUserData) );
}

这个错误的原因可能是什么?

最佳答案

该部分是检查堆有效性的方法,它不是问题的根源。

您很可能正在处理内存损坏,这可能很难调试。最好的办法是运行内存分析器工具(例如 Purify),或者如果代码库很小,请自己查看它,删除部分直到找到源代码。

当然,任何一种未定义的行为都可能导致这种情况。最常见的来源是:

  • 忘记从定义返回类型的函数返回
  • 多次删除对象
  • 通过指向没有virtual析构函数的基类的指针删除派生对象
  • 强制使用无效(C 风格强制转换,const_castreinterpret_cast)
  • 访问不属于您的内存(超出分配的内存范围写入)
  • 等(可以在这里添加)

虽然我会从一个完整的重建开始......你永远不知道!

关于c++ - 如何修复堆错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10652481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com