diff --git a/platforms/common/uORB/uORBDeviceMaster.cpp b/platforms/common/uORB/uORBDeviceMaster.cpp index f4e8223e1d..e753a270da 100644 --- a/platforms/common/uORB/uORBDeviceMaster.cpp +++ b/platforms/common/uORB/uORBDeviceMaster.cpp @@ -99,19 +99,11 @@ int uORB::DeviceMaster::advertise(const struct orb_metadata *meta, bool is_adver *instance = group_tries; } - /* driver wants a permanent copy of the path, so make one here */ - const char *devpath = strdup(nodepath); - - if (devpath == nullptr) { - return -ENOMEM; - } - /* construct the new node, passing the ownership of path to it */ - uORB::DeviceNode *node = new uORB::DeviceNode(meta, group_tries, devpath); + uORB::DeviceNode *node = new uORB::DeviceNode(meta, group_tries, nodepath); - /* if we didn't get a device, that's bad, free the path too */ + /* if we didn't get a device, that's bad */ if (node == nullptr) { - free((void *)devpath); return -ENOMEM; } diff --git a/platforms/common/uORB/uORBDeviceNode.cpp b/platforms/common/uORB/uORBDeviceNode.cpp index 431573c457..597e70e9a1 100644 --- a/platforms/common/uORB/uORBDeviceNode.cpp +++ b/platforms/common/uORB/uORBDeviceNode.cpp @@ -42,6 +42,10 @@ #include "uORBCommunicator.hpp" #endif /* ORB_COMMUNICATOR */ +#if defined(__PX4_NUTTX) +#include +#endif + static uORB::SubscriptionInterval *filp_to_subscription(cdev::file_t *filp) { return static_cast(filp->f_priv); } // round up to nearest power of two @@ -71,7 +75,7 @@ static inline uint8_t round_pow_of_two_8(uint8_t n) uORB::DeviceNode::DeviceNode(const struct orb_metadata *meta, const uint8_t instance, const char *path, uint8_t queue_size) : - CDev(path), + CDev(strdup(path)), // success is checked in CDev::init _meta(meta), _instance(instance), _queue_size(round_pow_of_two_8(queue_size)) @@ -82,7 +86,15 @@ uORB::DeviceNode::~DeviceNode() { delete[] _data; - CDev::unregister_driver_and_memory(); + const char *devname = get_devname(); + + if (devname) { +#if defined(__PX4_NUTTX) && !defined(CONFIG_BUILD_FLAT) + kmm_free((void *)devname); +#else + free((void *)devname); +#endif + } } int diff --git a/src/lib/cdev/CDev.cpp b/src/lib/cdev/CDev.cpp index ed668abf0e..de5e0482e9 100644 --- a/src/lib/cdev/CDev.cpp +++ b/src/lib/cdev/CDev.cpp @@ -128,6 +128,9 @@ CDev::init() if (ret == PX4_OK) { _registered = true; } + + } else { + ret = -ENODEV; } return ret; @@ -373,27 +376,4 @@ CDev::remove_poll_waiter(px4_pollfd_struct_t *fds) return -EINVAL; } -int CDev::unregister_driver_and_memory() -{ - int retval = PX4_OK; - - if (_registered) { - unregister_driver(_devname); - _registered = false; - - } else { - retval = -ENODEV; - } - - if (_devname != nullptr) { - free((void *)_devname); - _devname = nullptr; - - } else { - retval = -ENODEV; - } - - return retval; -} - } // namespace cdev diff --git a/src/lib/cdev/CDev.hpp b/src/lib/cdev/CDev.hpp index c98b8e39f8..071ae618fe 100644 --- a/src/lib/cdev/CDev.hpp +++ b/src/lib/cdev/CDev.hpp @@ -270,17 +270,6 @@ protected: px4_sem_t _lock; /**< lock to protect access to all class members (also for derived classes) */ - - /** - * First, unregisters the driver. Next, free the memory for the devname, - * in case it was expected to have ownership. Sets devname to nullptr. - * - * This is only needed if the ownership of the devname was passed to the CDev, otherwise ~CDev handles it. - * - * @return PX4_OK on success, -ENODEV if the devname is already nullptr - */ - int unregister_driver_and_memory(); - private: const char *_devname{nullptr}; /**< device node name */