From b48d4397af73be2be5845695ea1d8c6e120aa3ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Cab=C3=A9?= Date: Sun, 11 Oct 2026 00:58:09 +0000 Subject: [PATCH] fix(cyphal): initialize and free the CAN interface and heap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The CAN interface of the Cyphal driver was only set when a CAN interface is built in, but always closed and deleted without a check. The heap of libcanard was allocated with memalign(), which is not standard and only NuttX declares in stdlib.h, and freed with delete. The interface now starts as nullptr and is checked, and the heap is allocated with aligned_alloc() and freed with free(). The unused network and ioctl headers are no longer included. Assisted-by: Claude:opus-5.5 Signed-off-by: Benjamin Cabé --- src/drivers/cyphal/CanardHandle.cpp | 15 ++++++++------- src/drivers/cyphal/CanardHandle.hpp | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/drivers/cyphal/CanardHandle.cpp b/src/drivers/cyphal/CanardHandle.cpp index 26327131934..96a170c2403 100644 --- a/src/drivers/cyphal/CanardHandle.cpp +++ b/src/drivers/cyphal/CanardHandle.cpp @@ -33,8 +33,7 @@ #include "CanardHandle.hpp" -#include -#include +#include #include #include @@ -62,7 +61,7 @@ static void memFree(CanardInstance *const ins, void *const pointer) { o1heapFree CanardHandle::CanardHandle(uint32_t node_id, const size_t capacity, const size_t mtu_bytes) { - _cyphal_heap = memalign(O1HEAP_ALIGNMENT, HeapSize); + _cyphal_heap = aligned_alloc(O1HEAP_ALIGNMENT, HeapSize); cyphal_allocator = o1heapInit(_cyphal_heap, HeapSize, nullptr, nullptr); if (cyphal_allocator == nullptr) { @@ -87,11 +86,13 @@ CanardHandle::CanardHandle(uint32_t node_id, const size_t capacity, const size_t CanardHandle::~CanardHandle() { - _can_interface->close(); - delete _can_interface; - _can_interface = nullptr; + if (_can_interface) { + _can_interface->close(); + delete _can_interface; + _can_interface = nullptr; + } - delete static_cast(_cyphal_heap); + free(_cyphal_heap); _cyphal_heap = nullptr; } diff --git a/src/drivers/cyphal/CanardHandle.hpp b/src/drivers/cyphal/CanardHandle.hpp index 1e6c787c311..63682442e6d 100644 --- a/src/drivers/cyphal/CanardHandle.hpp +++ b/src/drivers/cyphal/CanardHandle.hpp @@ -76,7 +76,7 @@ public: void set_node_id(CanardNodeID id); private: - CanardInterface *_can_interface; + CanardInterface *_can_interface{nullptr}; CanardInstance _canard_instance;