以下代码显示DNS客户端中的缓存域名。有人可以帮我找到内存泄漏点击 int stat = DnsGetCacheDataTable(pEntry); 行吗?
PS:请在编译代码时使用DNSAPI.lib。
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <WinDNS.h>
#include <stdarg.h>
typedef struct _DNS_CACHE_ENTRY {
struct _DNS_CACHE_ENTRY* pNext; // Pointer to next entry
PWSTR pszName; // DNS Record Name
unsigned short wType; // DNS Record Type
unsigned short wDataLength; // Not referenced
unsigned long dwFlags; // DNS Record FlagsB
} DNSCACHEENTRY, *PDNSCACHEENTRY;
typedef int(WINAPI *DNS_GET_CACHE_DATA_TABLE)(PDNSCACHEENTRY);
void UpdateDNS(void)
{
PDNSCACHEENTRY pEntry = (PDNSCACHEENTRY) malloc(sizeof(DNSCACHEENTRY));
// Loading DLL
HINSTANCE hLib = LoadLibrary(TEXT("DNSAPI.dll"));
// Get function address
DNS_GET_CACHE_DATA_TABLE DnsGetCacheDataTable = (DNS_GET_CACHE_DATA_TABLE) GetProcAddress(hLib, "DnsGetCacheDataTable");
int stat = DnsGetCacheDataTable(pEntry);
printf("stat = %d\n", stat);
pEntry = pEntry->pNext;
while (pEntry) {
wprintf(L"%s : %d \n", (pEntry->pszName), (pEntry->wType));
pEntry = pEntry->pNext;
}
free(pEntry);
}
int main(int argc, char **argv) {
while (TRUE)
{
Sleep(100);
UpdateDNS();
}
return 0;
}
答案 0 :(得分:2)
此代码存在一些问题。
考虑到您在开始时调用LoadLibrary
而未在结束时调用FreeLibrary
。虽然技术上不是内存泄漏,但它可能不是最明智的想法......
考虑到在循环之前直接转到pEntry->pNext
,您正在跳过条目。当您分配malloc
返回的值时,您的内存泄漏发生在同一行代码中:
PDNSCACHEENTRY pEntry = (PDNSCACHEENTRY) malloc(sizeof(DNSCACHEENTRY));
/* ... */
pEntry = pEntry->pNext;
你甚至不需要malloc
,但更糟糕的是,你应该只free
malloc
个free(pEntry);
返回的值,这会导致错误:
malloc
事实上,您不仅不需要free
(或DnsRecordListFree
),而且您需要的是PDNS_RECORD entry;
HINSTANCE hLib = LoadLibrary(TEXT("DNSAPI.dll"));
DNS_GET_CACHE_DATA_TABLE DnsGetCacheDataTable = (DNS_GET_CACHE_DATA_TABLE) GetProcAddress(hLib, "DnsGetCacheDataTable");
int stat = DnsGetCacheDataTable(&entry);
printf("stat = %d\n", stat);
for (DNSCACHEENTRY *pTemp = &entry; pTemp; pTemp = pTemp->pNext) {
wprintf(L"%s : %d \n", pTemp->pszName, pTemp->wType);
}
DnsRecordListFree(entry, DnsFreeRecordList);
。
以下是您的代码应该是什么样子:
- (void)itemClicked:(id)sender {
NSAlert * alert = [NSAlert alertWithMessageText:@"Toggle Acknowledge"
defaultButton:@"Gotcha!"
alternateButton:nil
otherButton:nil
informativeTextWithFormat:@"NSStatusItem was clicked"]
[alert runModal] }
答案 1 :(得分:2)
然后开始调试......当然!你没有原始的pEntry而是修改过的。
这里是更正后的代码:
void UpdateDNS(void)
{
PDNSCACHEENTRY pEntry = (PDNSCACHEENTRY) malloc(sizeof(DNSCACHEENTRY) + 0x10000);
PDNSCACHEENTRY pFirstEntry = pEntry;
// Loading DLL
HINSTANCE hLib = LoadLibrary(TEXT("DNSAPI.dll"));
// Get function address
DNS_GET_CACHE_DATA_TABLE DnsGetCacheDataTable = (DNS_GET_CACHE_DATA_TABLE) GetProcAddress(hLib, "DnsGetCacheDataTable");
int stat = DnsGetCacheDataTable(pEntry);
printf("stat = %d\n", stat);
pEntry = pEntry->pNext;
while (pEntry) {
wprintf(L"%s : %d \n", (pEntry->pszName), (pEntry->wType));
pEntry = pEntry->pNext;
}
free(pFirstEntry);
}
更新:事实上,您不需要分配任何内存,因为DnsGetCacheDataTable会自行分配。尝试使用DnsRecordListFree释放内存,但似乎它不起作用。仍有泄漏:
最后我得到了不会泄漏的代码:
typedef int(WINAPI *DNS_GET_CACHE_DATA_TABLE)(PDNSCACHEENTRY*);
typedef void (WINAPI *P_DnsApiFree)(PVOID pData);
void UpdateDNS(void)
{
PDNSCACHEENTRY pEntry = NULL;
// Loading DLL
HINSTANCE hLib = LoadLibrary(TEXT("DNSAPI.dll"));
// Get function address
DNS_GET_CACHE_DATA_TABLE DnsGetCacheDataTable = (DNS_GET_CACHE_DATA_TABLE)GetProcAddress(hLib, "DnsGetCacheDataTable");
P_DnsApiFree pDnsApiFree = (P_DnsApiFree)GetProcAddress(hLib, "DnsApiFree");
int stat = DnsGetCacheDataTable(&pEntry);
PVOID pFirstEntry = pEntry;
printf("stat = %d\n", stat);
pEntry = pEntry->pNext;
while (pEntry) {
wprintf(L"%s : %d \n", (pEntry->pszName), (pEntry->wType));
pDnsApiFree(pEntry->pszName);
PVOID p = pEntry;
pEntry = pEntry->pNext;
pDnsApiFree(p);
}
}