HeapSetInformation return ERROR_INVALID_PARAMETER

HeapSetInformation return ERROR_INVALID_PARAMETER

本文关键字:INVALID PARAMETER ERROR return HeapSetInformation      更新时间:2023-10-16

我使用visual studio 2008 service Pack 1(9.0.30729.1)、windows 7专业版64位和8GB内存运行我的应用程序

我的应用程序是32位进程和使用/LARGEADDRESSAWARE选项

为了减少内存碎片,我调整了示例代码(http://msdn.microsoft.com/en-us/library/windows/desktop/aa366705(v=vs.85).aspx)

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#define HEAP_LFH 2
int __cdecl _tmain()
{
BOOL bResult;
HANDLE hHeap;
ULONG HeapInformation;
//
// Enable heap terminate-on-corruption. 
// A correct application can continue to run even if this call fails, 
// so it is safe to ignore the return value and call the function as follows:
// (void)HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
// If the application requires heap terminate-on-corruption to be enabled, 
// check the return value and exit on failure as shown in this example.
//
bResult = HeapSetInformation(NULL,
HeapEnableTerminationOnCorruption,
NULL,
0);
if (bResult != FALSE) {
_tprintf(TEXT("Heap terminate-on-corruption has been enabled.n"));
}
else {
_tprintf(TEXT("Failed to enable heap terminate-on-corruption with LastError %d.n"),
GetLastError());
return 1;
}
//
// Create a new heap with default parameters.
//
hHeap = HeapCreate(0, 0, 0);
if (hHeap == NULL) {
_tprintf(TEXT("Failed to create a new heap with LastError %d.n"),
GetLastError());
return 1;
}
//
// Enable the low-fragmenation heap (LFH). Starting with Windows Vista, 
// the LFH is enabled by default but this call does not cause an error.
//
HeapInformation = HEAP_LFH;
bResult = HeapSetInformation(hHeap,
HeapCompatibilityInformation,
&HeapInformation,
sizeof(HeapInformation));
if (bResult != FALSE) {
_tprintf(TEXT("The low-fragmentation heap has been enabled.n"));
}
else {
_tprintf(TEXT("Failed to enable the low-fragmentation heap with LastError %d.n"),
GetLastError());
return 1;
}
return 0;
}

bResult=HeapSetInformation(hHeap,堆兼容性信息,&堆信息,sizeof(堆信息));

bResult为false,getLastError为87(ERROR_INVALID_PARAMETER)

如何解决这个问题?

如果使用堆调试,也无法启用LFH调试中的工具适用于Windows或Microsoft应用程序的工具验证人。

在VS2010调试器下运行链接的示例代码会产生完全相同的失败。在不进行调试的情况下运行(Ctrl-F5),它会按预期成功。

然而,来自同一篇文章:

本主题中的信息适用于Windows Server 2003和Windows XP。从Windows Vista开始,系统使用低碎片堆(LFH),根据需要为内存分配提供服务请求应用程序不需要为其堆

因此,基本上,最好的解决方案是完全不这样做

如果真的,真的,真正的,必须特别支持XP/2k3,即使它们即将结束;

要在调试器下运行时启用低碎片堆,请将_NO_DEBUG_EAP环境变量设置为1。