Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pointer overflow and memory leak #8

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Inc/firmware/microros_allocators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
#include <cstdint>
#include <cstdlib>

uint32_t max_used_heap();
void free_all_heap();
uint32_t heap_get_current_pointer();
void heap_free_all();
void heap_set_current_pointer(uint32_t pointer);

void* microros_allocate(size_t size, void* state);
void microros_deallocate(void* pointer, void* state);
void* microros_reallocate(void* pointer, size_t size, void* state);
void* microros_zero_allocate(size_t number_of_elements, size_t size_of_element,
void* state);
void* state);
7 changes: 6 additions & 1 deletion Src/firmware/mainf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ static std::atomic_bool publish_param_trigger(true);
static bool mecanum_wheels = false;
static std::atomic_bool controller_replacement(false);

static uint32_t reset_pointer_position;

#define WHEEL_WRAPPER(NAME) \
constexpr const char* NAME##_cmd_pwm_topic = \
"~/wheel_" #NAME "/cmd_pwm_duty"; \
Expand Down Expand Up @@ -367,7 +369,7 @@ static void finiROS() {
(void)!rcl_init_options_fini(&init_options);
rclc_support_fini(&support);

free_all_heap();
heap_free_all();
}

static uint8_t uart_rbuffer[2048];
Expand Down Expand Up @@ -401,6 +403,7 @@ void setup() {

void initController() {
mecanum_wheels = params.mecanum_wheels;
reset_pointer_position = heap_get_current_pointer();
if (mecanum_wheels) {
rclc_publisher_init_best_effort(
&wheel_odom_mecanum_pub, &node,
Expand All @@ -424,6 +427,8 @@ void finiController() {
} else {
(void)!rcl_publisher_fini(&wheel_odom_pub, &node);
}
controller->~RobotController();
heap_set_current_pointer(reset_pointer_position);
}

void loop() {
Expand Down
10 changes: 7 additions & 3 deletions Src/firmware/microros_allocators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
static uint8_t heap[UROS_HEAP_SIZE];
static size_t current_pointer = 0;

uint32_t max_used_heap() {
uint32_t heap_get_current_pointer() {
return current_pointer;
}

void free_all_heap() {
void heap_free_all() {
current_pointer = 0;
}

void heap_set_current_pointer(uint32_t pointer) {
current_pointer = pointer;
}

void assert_position() {
if (current_pointer >= sizeof(heap)) {
while (1) {
Expand Down Expand Up @@ -56,7 +60,7 @@ void *microros_reallocate(void *pointer, size_t size, void *state) {
}

void *microros_zero_allocate(size_t number_of_elements, size_t size_of_element,
void *state) {
void *state) {
size_t size = number_of_elements * size_of_element;

if (size % 4 != 0) {
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ board_microros_distro = humble-fictionlab
board_microros_transport = serial
board_microros_user_meta = colcon.meta
lib_deps =
https://github.com/fictionlab/diff_drive_lib.git#1.5
https://github.com/fictionlab/diff_drive_lib.git#1.6
https://github.com/fictionlab/micro_ros_platformio#fictionlab-v1
lib_compat_mode = off

Expand Down
Loading