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

Memory issue when adding and removing time jump callbacks #293

Closed
greimela-si opened this issue Sep 6, 2018 · 1 comment
Closed

Memory issue when adding and removing time jump callbacks #293

greimela-si opened this issue Sep 6, 2018 · 1 comment
Assignees

Comments

@greimela-si
Copy link

Bug report

Required Info:

Steps to reproduce issue

  1. Create an instance of the rcl clock struct
  2. Add some time jump handler
  3. Remove this time jump handler
  4. Add a new time jump handler

Condensed example:

rcl_clock_add_jump_callback(&rcl_clock_, threshold, Clock::on_time_jump, some_handler);

rcl_clock_remove_jump_callback(&rcl_clock_, rclcpp::Clock::on_time_jump, some_handler)

rcl_clock_add_jump_callback(&rcl_clock_, threshold, Clock::on_time_jump, new_handler);

Expected behavior

The new time jump handler is registered in the clock.

Actual behavior

The new time jump handler is not registered in the clock, allocator.realloc() fails inside rcl_clock_add_jump_callback().
I suppose this originates in rcl_clock.jump_callbacks pointing to freed memory after rcl_clock_remove_jump_callback() has been called.
Reallocating a pointer that points to freed memory seems to be undefined behavior according to cppreference.

Additional information

One possible fix for this issue could be to use malloc() instead of realloc() in rcl_clock_add_jump_callback() if rcl_clock.num_jump_callbacks == 0.

@sloretz I think this functionality has been introduced in #284, any ideas?

@sloretz
Copy link
Contributor

sloretz commented Sep 6, 2018

Oops. I think the bug could be fixed by setting rcl_clock.jump_callbacks to NULL when removing the last callback. allocator.reallocate(NULL, ...) is required to succeed which I think is meant to say it behaves like stdlib realloc and is the same as calling malloc().

@sloretz sloretz self-assigned this Sep 6, 2018
@sloretz sloretz added in progress Actively being worked on (Kanban column) in review Waiting for review (Kanban column) and removed in progress Actively being worked on (Kanban column) labels Sep 6, 2018
@sloretz sloretz removed the in review Waiting for review (Kanban column) label Sep 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants