diff options
author | Jos Dehaes <jos.dehaes@gmail.com> | 2021-10-11 22:19:25 +0200 |
---|---|---|
committer | Jos Dehaes <jos.dehaes@gmail.com> | 2021-10-11 22:19:25 +0200 |
commit | 28cb67753332fe8c93d23cbf8e4db636fc6823d6 (patch) | |
tree | 44749e6144cc3aa3f2e2f85bc5202b7fd05b5b89 /src/osx | |
parent | 304457863f6cdccf82fbe1cca3078c7f5d9f97cd (diff) |
more memory free-up - still leaks like crazy
Diffstat (limited to 'src/osx')
-rw-r--r-- | src/osx/btop_collect.cpp | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/src/osx/btop_collect.cpp b/src/osx/btop_collect.cpp index bbb2def..e4918fc 100644 --- a/src/osx/btop_collect.cpp +++ b/src/osx/btop_collect.cpp @@ -841,24 +841,27 @@ namespace Net { size_t len; if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) { Logger::error("failed getting network interfaces"); - } - char *buf = (char *)malloc(len); - if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) { - Logger::error("failed getting network interfaces"); - } - char *lim = buf + len; - char *next = NULL; - for (next = buf; next < lim;) { - struct if_msghdr *ifm = (struct if_msghdr *)next; - next += ifm->ifm_msglen; - if (ifm->ifm_type == RTM_IFINFO2) { - struct if_msghdr2 *if2m = (struct if_msghdr2 *)ifm; - struct sockaddr_dl *sdl = (struct sockaddr_dl *)(if2m + 1); - char iface[32]; - strncpy(iface, sdl->sdl_data, sdl->sdl_nlen); - iface[sdl->sdl_nlen] = 0; - ifstats[iface] = std::tuple(if2m->ifm_data.ifi_ibytes, if2m->ifm_data.ifi_obytes); + } else { + char *buf = (char *)malloc(len); + if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) { + Logger::error("failed getting network interfaces"); + } else { + char *lim = buf + len; + char *next = NULL; + for (next = buf; next < lim;) { + struct if_msghdr *ifm = (struct if_msghdr *)next; + next += ifm->ifm_msglen; + if (ifm->ifm_type == RTM_IFINFO2) { + struct if_msghdr2 *if2m = (struct if_msghdr2 *)ifm; + struct sockaddr_dl *sdl = (struct sockaddr_dl *)(if2m + 1); + char iface[32]; + strncpy(iface, sdl->sdl_data, sdl->sdl_nlen); + iface[sdl->sdl_nlen] = 0; + ifstats[iface] = std::tuple(if2m->ifm_data.ifi_ibytes, if2m->ifm_data.ifi_obytes); + } + } } + free(buf); // no idea how to RAII this with the funky/sketchy pointer arithmethic } //? Get total recieved and transmitted bytes + device address if no ip was found |