Skip to content

freeing Time members in destructor, adding copy constructor / assignment operator - #362

Merged
Karsten1987 merged 6 commits into
masterfrom
fix_win_debug
Aug 24, 2017
Merged

Karsten1987 merged 6 commits into
masterfrom
fix_win_debug

Conversation

@Karsten1987

@Karsten1987 Karsten1987 commented Aug 23, 2017 •

Copy link
Copy Markdown
Contributor

Connects to #357

add a copy constructor to the rclcpp::Time class which is necessary to fix the windows debug tests.
I tested it locally on my windows machine.

CI:
Build Status

@Karsten1987 Karsten1987 added the in review Waiting for review (Kanban column) label Aug 23, 2017
@Karsten1987 Karsten1987 self-assigned this Aug 23, 2017
@dhood

dhood commented Aug 24, 2017

Copy link
Copy Markdown
Member

there's a CI_CMAKE_BUILD_TYPE option in the jobs that can be set to debug

rclcpp::exceptions::throw_from_rcl_error(
ret, "could not get current time stamp");
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: Why?

Comment thread rclcpp/test/test_time.cpp
rclcpp::Time system_time(1, 0, RCL_SYSTEM_TIME);
rclcpp::Time steady_time(2, 0, RCL_STEADY_TIME);
rclcpp::Time system_time(0, 0, RCL_SYSTEM_TIME);
rclcpp::Time steady_time(0, 0, RCL_STEADY_TIME);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of this change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to demonstrate that even though the timestamps are equal, the operator== fails because of the different time sources.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should just be added as an additional test? Sounds like a good distinction.

@dirk-thomas

Copy link
Copy Markdown
Member

I think there is something missing here. The patch tries to fix a memory ownership problem which is likely not specific to Windows Debug.

The underlying rcl_time_source_t stores a data * and rcl_time_point_t stores a rcl_time_source_t *. Without an explicit copy constructor / assignment operator the original Time instance as well as the copy will implicitly share the same underlying pointers which have been allocated by the original time instance.

It is certainly good to add the copy constructor / assignment operator to make the two Time instances independent from each other. But the fact that the lack of those results in a problem on Windows Debug indicates that something else is wrong with the memory.

While most of the time sources only set the data pointer to something "fixed" RCL_ROS_TIME will actually allocate memory for it. The fact that an example copy constructing a time with that source doesn't fail with a double free error indicates that the memory is never freed. That is easily demonstrated with a mini example in valgrind (unrelated to this patch):

rclcpp::Time t(1, 2, RCL_ROS_TIME);
16 bytes in 1 blocks are definitely lost in loss record 1 of 2
   at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
   by 0x6801A46: rcl_ros_time_source_init (time.c:135)
   by 0x680193C: rcl_time_source_init (time.c:103)
   by 0x51C4205: (anonymous namespace)::init_time_source(rcl_time_source_type_t) (time.cpp:34)
   by 0x51C45D3: rclcpp::Time::Time(int, unsigned int, rcl_time_source_type_t) (time.cpp:<line>)
   ...

So as a conclusion the destructor of Time should be fixed first to actually finiing the underlying rcl structures correctly. After that the above example should not leak anymore. After that I would expect "double free" errors due to sharing of the same underlying rcl resources. This is where this patch comes in handy which should create truely independent instance.

@Karsten1987

Copy link
Copy Markdown
Contributor Author

Having looked at the rcl_time_ implementation, I indeed forgot to cleanup the resources correctly. The last commit should have fixed that.

Running CI again:

win debug: Build Status
win release: Build Status

for comparison:
linux debug: Build Status
linux release: Build Status

@dirk-thomas dirk-thomas changed the title copy constructor for fixing windows debug freeing Time members in destructor, adding copy constructor / assignment operator Aug 24, 2017

@dirk-thomas dirk-thomas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also please add tests for the problematic cases in order to ensure they all pass now as well as in the future.


void
Time::operator=(const Time & rhs)
{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you try that with a RCL_ROS_TIME? Since it internally allocates data within the rcl_time_source_t I would expect that this can't be just copied here.

Comment thread rclcpp/test/test_time.cpp Outdated

for (auto time_source : {RCL_ROS_TIME, RCL_SYSTEM_TIME, RCL_STEADY_TIME}) {
rclcpp::Time time = rclcpp::Time(0, 0, time_source);
rclcpp::Time copy_constructor_time = time;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be rclcpp::Time copy_constructor_time(time) if you want to call the copy constructor.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line will also call the copy constructor.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, maybe still update the line to make the intention clear?

@Karsten1987

Copy link
Copy Markdown
Contributor Author

Latest CI for windows debug:

Build Status

@dirk-thomas dirk-thomas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Did you try running test_time in valgrind?

@Karsten1987

Copy link
Copy Markdown
Contributor Author

MacOS memory sanitizer doesn't complain, so I'll take this as a valid outcome.

@Karsten1987
Karsten1987 merged commit 8e2e64e into master Aug 24, 2017
@Karsten1987
Karsten1987 deleted the fix_win_debug branch August 24, 2017 22:21
@Karsten1987 Karsten1987 removed the in review Waiting for review (Kanban column) label Aug 24, 2017
DensoADAS pushed a commit to DensoADAS/rclcpp that referenced this pull request Aug 5, 2022
* fix rosbag2_tests resources and play_end_to_end test

Signed-off-by: Mabel Zhang <[email protected]>

* remove comment

Signed-off-by: Mabel Zhang <[email protected]>

* add colon after End in info

Signed-off-by: Mabel Zhang <[email protected]>

* fix test for buildfarm linux failure

Signed-off-by: Mabel Zhang <[email protected]>

* fix timestamp for mac test failure

Signed-off-by: Mabel Zhang <[email protected]>
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

Successfully merging this pull request may close these issues.

5 participants