在此先感谢所有试图帮助我的人。我在这里遇到了一个大问题,我找到了一些关于 sysctl 的示例代码并对其进行了扩展,以便我可以查询网络接口的输入/输出数据。当我直接在 main() 中运行此代码(没有任何 NSAutoreleasePool)时,一切正常。但是,一旦我将它添加到我的班级并执行它,我就会得到总线错误和段错误。我已将问题追溯到 NSAutoreleasePools。有人可以帮忙吗?(如果您不相信,只需在代码之前放置一个 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 并将整个内容放入 main())
int mib[] = { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
int alloc;
struct if_msghdr *ifm, *nextifm;
struct sockaddr_dl *sdl;
char *lim, *next;
size_t needed;
char s[32];
char* buf;
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
return 0;
if (alloc < needed) {
buf = malloc(needed);
if (buf == NULL)
return 0;
alloc = needed;
}
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
return 0;
lim = buf + needed;
next = buf;
while (next < lim) {
ifm = (struct if_msghdr *)next;
if (ifm->ifm_type != RTM_IFINFO)
return 0;
next += ifm->ifm_msglen;
while (next < lim) {
nextifm = (struct if_msghdr *)next;
if (nextifm->ifm_type != RTM_NEWADDR)
break;
next += nextifm->ifm_msglen;
}
if (ifm != NULL && ifm->ifm_flags & IFF_UP) {
sdl = (struct sockaddr_dl *)(ifm + 1);
if (sdl->sdl_family != AF_LINK)
continue;
strncpy(s, sdl->sdl_data, sdl->sdl_nlen);
s[sdl->sdl_nlen] = '\0';
NSLog(@"interface %s in %qu out %qu \n", s,(UInt64)ifm->ifm_data.ifi_ibytes, (UInt64)ifm->ifm_data.ifi_obytes );
}
}