gpt4 book ai didi

c++ - HeapSetInformation 返回 ERROR_INVALID_PARAMETER

转载 作者:行者123 更新时间:2023-11-28 03:00:50 32 4
gpt4 key购买 nike

我使用 visual studio 2008 service pack1(9.0.30729.1)、windows 7 professional 64 位和 8 GB 内存运行我的应用程序

我的应用程序是 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)

如何解决这个问题?

最佳答案

The LFH also cannot be enabled if you are using the heap debugging tools in Debugging Tools for Windows or Microsoft Application Verifier.

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

但是,来自同一篇文章:

The information in this topic applies to Windows Server 2003 and Windows XP. Starting with Windows Vista, the system uses the low-fragmentation heap (LFH) as needed to service memory allocation requests. Applications do not need to enable the LFH for their heaps.

所以基本上,最好的解决办法是根本不这样做

如果您真的、真的、真的必须特别支持 XP/2k3,即使它们的终结迫在眉睫;

To enable the low-fragmentation heap when running under a debugger, set the _NO_DEBUG_HEAP environment variable to 1.

关于c++ - HeapSetInformation 返回 ERROR_INVALID_PARAMETER,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20894473/

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