c++ CRT检测到应用程序在堆缓冲区结束后写入内存
C++ CRT detected that the application wrote to memory after end of heap buffer
这是我的函数,它应该找到第一个遇到的具有给定名称的进程,并返回一个句柄。然而,在这个过程中,我需要在堆上分配一些数据,当我试图删除时抛出一个错误。
HANDLE GetProcessHandleByName(CHAR procName[])
{
DWORD pProcessIds[1024];
DWORD pBytesReturned;
::EnumProcesses(pProcessIds, sizeof(pProcessIds), &pBytesReturned);
int noOfProcs = pBytesReturned / sizeof(DWORD);
if (noOfProcs)
{
for (int i = 0; i < noOfProcs; i++)
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, pProcessIds[i]);
if (!hProcess) continue;
HMODULE hMod;
DWORD cbNeeded;
CHAR strBuffer[MAX_PATH];
if (::EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded))
{
auto length = ::GetModuleBaseName(hProcess, hMod, strBuffer, sizeof(strBuffer) / sizeof(CHAR));
CHAR *str = new CHAR[length];
::strcpy(str, strBuffer);
if (::strcmp(str, procName) == 0)
{
delete[] str; //can't delete -> Exception CRT detected that the application wrote to memory after end of heap buffer.
return hProcess;
}
}
}
}
}
您不应该分配、复制和删除它。此外,如果::strcmp(str, procName) != 0
.
试试这个:
if (::EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded))
{
auto length = ::GetModuleBaseName(hProcess, hMod, strBuffer, sizeof(strBuffer) / sizeof(CHAR));
if (::strcmp(strBuffer, procName) == 0)
{
return hProcess;
}
}
相关文章:
- 如何在函数结束时有效地释放矢量内存
- CRT 检测到应用程序在堆缓冲区(新建/删除)类结束后写入内存
- 删除不会在函数结束时释放内存
- 如何使商店成为内存的开始和结束,以获得最佳拟合
- 动态内存分配 - 为什么程序结束时没有"delete"?
- 当c++程序结束时,指针会从内存中释放吗
- 主进程结束后C++内存泄漏
- 如何修复此错误?CRT检测到应用程序在堆缓冲区结束后写入内存
- 使用运算符 new/malloc 分配的内存块是否可以在程序执行结束后持续存在
- 强制程序在函数结束 (C++) 后释放在函数期间分配的内存
- C++数组能否在内存边界结束
- 当我们开始覆盖内存时,对象的生命周期是否已经结束?
- c++ CRT检测到应用程序在堆缓冲区结束后写入内存
- 在c++中,如果一个对象是在一个方法中创建的,那么该对象使用的内存是在该方法结束后自动释放的
- 在程序结束后,内存泄漏有任何影响吗?
- 使用自定义的超分配分配器利用std::vector结束后的内存
- 变量或对象的内存在程序结束时自动终止,而不是使用析构函数
- 在c++程序结束时返回内存
- 字符串结束字符会损坏内存吗?缓冲区[大小]='\0';
- 在<int>代码块结束之前释放分配给 std::vector 的内存的正确方法