-
Notifications
You must be signed in to change notification settings - Fork 255
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
Fixes bag not closing properly (#1586) #1590
Changes from all commits
dd4db46
2f570e7
e495ec2
f279046
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,7 +61,10 @@ SequentialWriter::SequentialWriter( | |
|
||
SequentialWriter::~SequentialWriter() | ||
{ | ||
close(); | ||
// only close writer if bag is open to prevent overwrite | ||
if (storage_) { | ||
close(); | ||
} | ||
} | ||
|
||
void SequentialWriter::init_metadata() | ||
|
@@ -161,8 +164,9 @@ void SequentialWriter::close() | |
metadata_io_->write_metadata(base_folder_, metadata_); | ||
} | ||
|
||
storage_.reset(); // Necessary to ensure that the storage is destroyed before the factory | ||
storage_factory_.reset(); | ||
// destroy current storage object and clear active track map for topic metadata | ||
storage_.reset(); | ||
topics_names_to_info_.clear(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about |
||
} | ||
|
||
void SequentialWriter::create_topic(const rosbag2_storage::TopicMetadata & topic_with_type) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,8 +43,10 @@ TEST(TestRosbag2CPPAPI, minimal_writer_example) | |
serialization.serialize_message(&test_msg, &serialized_msg); | ||
|
||
auto rosbag_directory = rcpputils::fs::path("test_rosbag2_writer_api_bag"); | ||
auto rosbag_directory_next = rcpputils::fs::path("test_rosbag2_writer_api_bag_next"); | ||
// in case the bag was previously not cleaned up | ||
rcpputils::fs::remove_all(rosbag_directory); | ||
rcpputils::fs::remove_all(rosbag_directory_next); | ||
|
||
{ | ||
rosbag2_cpp::Writer writer; | ||
|
@@ -87,7 +89,23 @@ TEST(TestRosbag2CPPAPI, minimal_writer_example) | |
// writing a non-serialized message | ||
writer.write(test_msg, "/a/ros2/message", rclcpp::Clock().now()); | ||
|
||
// close on scope exit | ||
// close as prompted | ||
writer.close(); | ||
|
||
// open a new bag with the same writer | ||
writer.open(rosbag_directory_next.string()); | ||
|
||
// another simple serialized message | ||
std::shared_ptr<rclcpp::SerializedMessage> serialized_msg3 = | ||
std::make_shared<rclcpp::SerializedMessage>(); | ||
serialization.serialize_message(&test_msg, serialized_msg3.get()); | ||
Comment on lines
+98
to
+101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't bother creating a new serialized message, I would recommend using the same |
||
|
||
// write same topic to different bag | ||
writer.write( | ||
serialized_msg3, "/yet/another/topic", "test_msgs/msg/BasicTypes", | ||
rclcpp::Clock().now()); | ||
|
||
// close by scope | ||
} | ||
|
||
{ | ||
|
@@ -114,6 +132,25 @@ TEST(TestRosbag2CPPAPI, minimal_writer_example) | |
// close on scope exit | ||
} | ||
|
||
// read the other written bag | ||
{ | ||
rosbag2_cpp::Reader reader; | ||
std::string topic; | ||
reader.open(rosbag_directory_next.string()); | ||
ASSERT_TRUE(reader.has_next()); | ||
|
||
auto bag_message = reader.read_next(); | ||
topic = bag_message->topic_name; | ||
|
||
TestMsgT extracted_test_msg; | ||
rclcpp::SerializedMessage extracted_serialized_msg(*bag_message->serialized_data); | ||
serialization.deserialize_message( | ||
&extracted_serialized_msg, &extracted_test_msg); | ||
|
||
EXPECT_EQ(test_msg, extracted_test_msg); | ||
EXPECT_EQ("/yet/another/topic", topic); | ||
} | ||
|
||
// alternative reader | ||
{ | ||
rosbag2_cpp::Reader reader; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this is right thing to do.
storage_factory_
lifetime should be aligned withWriter
until it is destructed.