Skip to content

Commit

Permalink
Copy attachments buffer when parsing summary (#100)
Browse files Browse the repository at this point in the history
* Copy attachments buffer when parsing summary

Signed-off-by: Juan Lopez Fernandez <juanlopez@eprosima.com>

* Apply suggestion

Signed-off-by: Juan Lopez Fernandez <juanlopez@eprosima.com>

---------

Signed-off-by: Juan Lopez Fernandez <juanlopez@eprosima.com>
  • Loading branch information
juanlofer-eprosima authored Dec 12, 2023
1 parent 06d37fe commit 70097cc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions thirdparty/mcap/mcap/reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ class MCAP_PUBLIC McapReader final {
bool parsedSummary_ = false;

void reset_();
void clear_attachments_();
Status readSummarySection_(IReadable& reader);
Status readSummaryFromScan_(IReadable& reader);
};
Expand Down
18 changes: 15 additions & 3 deletions thirdparty/mcap/mcap/reader.inl
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ void McapReader::reset_() {
statistics_ = std::nullopt;
chunkIndexes_.clear();
attachmentIndexes_.clear();
clear_attachments_();
schemas_.clear();
channels_.clear();
dataStart_ = 0;
Expand All @@ -360,6 +361,13 @@ void McapReader::reset_() {
parsedSummary_ = false;
}

void McapReader::clear_attachments_() {
for (auto& [attachment_name, attachment] : attachments_) {
std::free((void*)attachment.data);
}
attachments_.clear();
}

Status McapReader::readSummary(ReadSummaryMethod method, const ProblemCallback& onProblem) {
if (!input_) {
const Status status{StatusCode::NotOpen};
Expand Down Expand Up @@ -427,7 +435,7 @@ Status McapReader::readSummarySection_(IReadable& reader) {
}

attachmentIndexes_.clear();
attachments_.clear();
clear_attachments_();
metadataIndexes_.clear();
metadata_.clear();
chunkIndexes_.clear();
Expand Down Expand Up @@ -488,7 +496,7 @@ Status McapReader::readSummaryFromScan_(IReadable& reader) {
schemas_.clear();
channels_.clear();
attachmentIndexes_.clear();
attachments_.clear();
clear_attachments_();
metadataIndexes_.clear();
metadata_.clear();
chunkIndexes_.clear();
Expand All @@ -503,7 +511,11 @@ Status McapReader::readSummaryFromScan_(IReadable& reader) {
typedReader.onAttachment = [&](const Attachment& attachment, ByteOffset fileOffset) {
AttachmentIndex attachmentIndex{attachment, fileOffset};
attachmentIndexes_.emplace(attachment.name, attachmentIndex);
attachments_.emplace(attachment.name, attachment);
// Copy buffer for storage as original is freed when leaving scope
Attachment attachment_copy = attachment;
attachment_copy.data = (std::byte*)std::malloc(attachment.dataSize);
std::memcpy((void*)attachment_copy.data, attachment.data, attachment.dataSize);
attachments_.emplace(attachment_copy.name, std::move(attachment_copy));
};
typedReader.onMetadata = [&](const Metadata& metadata, ByteOffset fileOffset) {
MetadataIndex metadataIndex{metadata, fileOffset};
Expand Down

0 comments on commit 70097cc

Please sign in to comment.