From 885fe62fec535169203bea727a03f4c3186661bb Mon Sep 17 00:00:00 2001 From: Jukka Laitinen Date: Tue, 9 Feb 2021 11:02:52 +0200 Subject: [PATCH] cdev: Fix deallocating devname in nuttx protected/kernel builds The CDevs always live in kernel side. In kernel the devname is allocated in kernel heap. Replace "free" with kmm_free on nuttx to correct the deallocation; this automatically maps to "free" on flat build. Define __KERNEL__ flag so that the proper nuttx headers are used for building. TODO: It is fishy in the first place that the devname is allocated in inherited classes but deleted in the CDev base class. Especially when the devname is often passed as a constant string (which cannot even be freed) by many drivers. The allocation and deallocation of devname should be done within the CDev base class instead. Signed-off-by: Jukka Laitinen --- src/lib/cdev/CDev.cpp | 8 ++++++++ src/lib/cdev/CMakeLists.txt | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/lib/cdev/CDev.cpp b/src/lib/cdev/CDev.cpp index ed668abf0e..9dc3be9cda 100644 --- a/src/lib/cdev/CDev.cpp +++ b/src/lib/cdev/CDev.cpp @@ -43,6 +43,10 @@ #include +#if defined(__PX4_NUTTX) +#include +#endif + namespace cdev { @@ -386,7 +390,11 @@ int CDev::unregister_driver_and_memory() } if (_devname != nullptr) { +#if defined(__PX4_NUTTX) && !defined(CONFIG_BUILD_FLAT) + kmm_free((void *)_devname); +#else free((void *)_devname); +#endif _devname = nullptr; } else { diff --git a/src/lib/cdev/CMakeLists.txt b/src/lib/cdev/CMakeLists.txt index 6a3f90fe9f..0d7d869fdf 100644 --- a/src/lib/cdev/CMakeLists.txt +++ b/src/lib/cdev/CMakeLists.txt @@ -49,7 +49,7 @@ px4_add_library(cdev CDev.hpp ${SRCS_PLATFORM} ) -target_compile_options(cdev PRIVATE ${MAX_CUSTOM_OPT_LEVEL}) +target_compile_options(cdev PRIVATE ${MAX_CUSTOM_OPT_LEVEL} -D__KERNEL__) if(PX4_TESTING) add_subdirectory(test)