我使用visual studio 2008 service pack1(9.0.30729.1),带有8 GB内存的Windows 7专业版64位运行我的应用程序
我的应用程序是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, HeapCompatibilityInformation, &安培; HeapInformation, 的sizeof(HeapInformation));
bResult为false且getLastError为87(ERROR_INVALID_PARAMETER)
如何解决这个问题?
答案 0 :(得分:2)
在VS2010调试器下运行链接的示例代码会对我产生完全相同的故障。在没有调试的情况下运行(Ctrl-F5),它会按预期成功。
然而,来自同一篇文章:
本主题中的信息适用于Windows Server 2003和 Windows XP。从Windows Vista开始,系统使用 服务内存分配所需的低碎片堆(LFH) 要求。 应用程序不需要为其启用LFH 堆
基本上,最好的解决方法是不要这样做。
如果你真的,真的,真的,必须 特别支持XP / 2k3,即使它们的结束迫在眉睫;
要在调试器下运行时启用低碎片堆,请将_NO_DEBUG_HEAP环境变量设置为1.