summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJos Dehaes <jos.dehaes@gmail.com>2021-10-11 22:19:25 +0200
committerJos Dehaes <jos.dehaes@gmail.com>2021-10-11 22:19:25 +0200
commit28cb67753332fe8c93d23cbf8e4db636fc6823d6 (patch)
tree44749e6144cc3aa3f2e2f85bc5202b7fd05b5b89
parent304457863f6cdccf82fbe1cca3078c7f5d9f97cd (diff)
more memory free-up - still leaks like crazy
-rw-r--r--src/osx/btop_collect.cpp37
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