gpt4 book ai didi

windows - GetIpAddrTable() 泄漏内存。如何解决?

转载 作者:可可西里 更新时间:2023-11-01 11:22:27 25 4
gpt4 key购买 nike

在我的 Windows 7 机器上,这个简单的程序会导致应用程序的内存使用量不断上升,没有上限。我已经删除了所有非必要的内容,看起来罪魁祸首显然是 Microsoft Iphlpapi 函数“GetIpAddrTable()”。在每次调用时,它都会泄漏一些内存。在循环中(例如检查网络接口(interface)列表的更改),它是不可持续的。似乎没有异步通知 API 可以完成这项工作,所以现在我面临着可能必须将此逻辑隔离到一个单独的进程中并定期回收该进程——一个丑陋的解决方案。

有什么想法吗?

// IphlpLeak.cpp - demonstrates that GetIpAddrTable leaks memory internally:  run this and watch
// the memory use of the app climb up continuously with no upper bound.
#include <stdio.h>
#include <windows.h>
#include <assert.h>
#include <Iphlpapi.h>
#pragma comment(lib,"Iphlpapi.lib")

void testLeak() {
static unsigned char buf[16384];
DWORD dwSize(sizeof(buf));
if (GetIpAddrTable((PMIB_IPADDRTABLE)buf, &dwSize, false) == ERROR_INSUFFICIENT_BUFFER)
{
assert(0); // we never hit this branch.
return;
}
}
int main(int argc, char* argv[]) {
for ( int i = 0; true; i++ ) {
testLeak();
printf("i=%d\n",i);
Sleep(1000);
}
return 0;
}

最佳答案

@稳定狗:我已经运行了 24 小时未修改的示例,但没有观察到程序的提交大小无限期增加。它始终保持在 1024 KB 以下。这是在 Windows 7(32 位,没有 Service Pack 1)上。

关于windows - GetIpAddrTable() 泄漏内存。如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2815943/

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