gpt4 book ai didi

ios - 内存永远不会释放潜在的内存泄漏

转载 作者:行者123 更新时间:2023-12-01 17:32:19 26 4
gpt4 key购买 nike

只需在我的应用程序上运行analyze,它就会抛出此内存错误,并在以下代码中指向return行:

int                 mgmtInfoBase[6];
char *msgBuffer = NULL;
size_t length;
unsigned char macAddress[6];
struct if_msghdr *interfaceMsgStruct;
struct sockaddr_dl *socketStruct;
NSString *errorFlag = NULL;

mgmtInfoBase[0] = CTL_NET; // Request network subsystem
mgmtInfoBase[1] = AF_ROUTE; // Routing table info
mgmtInfoBase[2] = 0;
mgmtInfoBase[3] = AF_LINK; // Request link layer information
mgmtInfoBase[4] = NET_RT_IFLIST; // Request all configured interfaces

if ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0)
errorFlag = @"if_nametoindex failure";
else
{
if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0)
errorFlag = @"sysctl mgmtInfoBase failure";
else
{
if ((msgBuffer = malloc(length)) == NULL)
errorFlag = @"buffer allocation failure";
else
{
if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)
errorFlag = @"sysctl msgBuffer failure";
}
}
}

if (errorFlag != NULL)
{
NSLog(@"Error: %@", errorFlag);
return errorFlag; // this line gives the memory leak warning
}

我对 C不太了解,希望有人能告诉我这里发生了什么。

最佳答案

您需要释放msgBuffer

 if ((msgBuffer = malloc(length)) == NULL)

也许你可以在返回之前做
if (errorFlag != NULL)
{
free (msgBuffer); // Free here
NSLog(@"Error: %@", errorFlag);
return errorFlag; // this line gives the memory leak warning
}

关于ios - 内存永远不会释放潜在的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16584027/

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