From 70097ccb954fd6b3b7efcd10f6a7b8ed4d8c548d Mon Sep 17 00:00:00 2001 From: juanlofer-eprosima <88179026+juanlofer-eprosima@users.noreply.github.com> Date: Tue, 12 Dec 2023 15:46:45 +0100 Subject: [PATCH] Copy attachments buffer when parsing summary (#100) * Copy attachments buffer when parsing summary Signed-off-by: Juan Lopez Fernandez * Apply suggestion Signed-off-by: Juan Lopez Fernandez --------- Signed-off-by: Juan Lopez Fernandez --- thirdparty/mcap/mcap/reader.hpp | 1 + thirdparty/mcap/mcap/reader.inl | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/thirdparty/mcap/mcap/reader.hpp b/thirdparty/mcap/mcap/reader.hpp index 1635afd76..561cfb13b 100644 --- a/thirdparty/mcap/mcap/reader.hpp +++ b/thirdparty/mcap/mcap/reader.hpp @@ -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); }; diff --git a/thirdparty/mcap/mcap/reader.inl b/thirdparty/mcap/mcap/reader.inl index 56433cfaf..5b51f9266 100644 --- a/thirdparty/mcap/mcap/reader.inl +++ b/thirdparty/mcap/mcap/reader.inl @@ -351,6 +351,7 @@ void McapReader::reset_() { statistics_ = std::nullopt; chunkIndexes_.clear(); attachmentIndexes_.clear(); + clear_attachments_(); schemas_.clear(); channels_.clear(); dataStart_ = 0; @@ -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}; @@ -427,7 +435,7 @@ Status McapReader::readSummarySection_(IReadable& reader) { } attachmentIndexes_.clear(); - attachments_.clear(); + clear_attachments_(); metadataIndexes_.clear(); metadata_.clear(); chunkIndexes_.clear(); @@ -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(); @@ -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};