From ff810164d562d38e37a6125ed614d4ac101164b3 Mon Sep 17 00:00:00 2001 From: Jukka Laitinen Date: Tue, 30 Jan 2024 12:46:06 +0200 Subject: [PATCH] src/systemcmds/uorb: Map every uorb instance just once Fix a memory leak in uorb systemcmd Signed-off-by: Jukka Laitinen --- src/systemcmds/uorb/uORBDeviceMaster.cpp | 33 ++++++++++++++---------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/systemcmds/uorb/uORBDeviceMaster.cpp b/src/systemcmds/uorb/uORBDeviceMaster.cpp index 1814f734c2c0..662a1d7a8d21 100644 --- a/src/systemcmds/uorb/uORBDeviceMaster.cpp +++ b/src/systemcmds/uorb/uORBDeviceMaster.cpp @@ -109,6 +109,7 @@ int uORB::DeviceMaster::addNewDeviceNodes(DeviceNodeStatisticsData **first_node, DIR *shm_dir = opendir(CONFIG_FS_SHMFS_VFS_PATH); struct dirent *shm; const char orb_name_prefix[] = "_orb_"; + char orb_name[orb_maxpath]; while ((shm = readdir(shm_dir)) != nullptr) { @@ -116,6 +117,25 @@ int uORB::DeviceMaster::addNewDeviceNodes(DeviceNodeStatisticsData **first_node, continue; } + // check if already added + cur_node = *first_node; + + while (cur_node) { + int instance = cur_node->node->get_instance(); + + if (uORB::Utils::node_mkpath(orb_name, cur_node->node->get_meta(), &instance)) { + PX4_ERR("Can't construct orb name?"); + continue; + } + + if (!strcmp(orb_name, shm->d_name)) { + // already added + continue; + } + + cur_node = cur_node->next; + } + void *ptr = nullptr; uORB::DeviceNode *node = nullptr; @@ -139,19 +159,6 @@ int uORB::DeviceMaster::addNewDeviceNodes(DeviceNodeStatisticsData **first_node, ++num_topics; - //check if already added - cur_node = *first_node; - - while (cur_node && cur_node->node != node) { - cur_node = cur_node->next; - } - - if (cur_node) { - // currently nuttx creates a new mapping on every mmap. TODO: check linux - px4_munmap(node, sizeof(uORB::DeviceNode)); - continue; - } - if (num_filters > 0 && topic_filter) { bool matched = false;