fix(cyphal): initialize and free the CAN interface and heap

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é <benjamin@zephyrproject.org>
This commit is contained in:
Benjamin Cabé authored and Ramon Roche committed 2026-10-14 11:55:19 +02:00
1 parent a53b5a615a
commit b48d4397af
2 files changed
+9 -8

No files matched your search

+8 -7
View File
@@ -33,8 +33,7 @@
#include "CanardHandle.hpp"
#include <net/if.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <string.h>
#include <px4_platform_common/log.h>
@@ -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<uint8_t *>(_cyphal_heap);
free(_cyphal_heap);
_cyphal_heap = nullptr;
}
+1 -1
View File
@@ -76,7 +76,7 @@ public:
void set_node_id(CanardNodeID id);
private:
CanardInterface *_can_interface;
CanardInterface *_can_interface{nullptr};
CanardInstance _canard_instance;