From c9410847ec669978b5e314c22e79105f27531e60 Mon Sep 17 00:00:00 2001 From: Juan Lopez Fernandez Date: Thu, 30 Nov 2023 12:52:39 +0100 Subject: [PATCH 1/2] Copy attachments buffer when parsing summary 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..5ca986604 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, attachment_copy); }; typedReader.onMetadata = [&](const Metadata& metadata, ByteOffset fileOffset) { MetadataIndex metadataIndex{metadata, fileOffset}; From 91da9852e8f2e10cd0c9a0a1e5bb61714aa8d74d Mon Sep 17 00:00:00 2001 From: Juan Lopez Fernandez Date: Tue, 12 Dec 2023 15:11:46 +0100 Subject: [PATCH 2/2] Apply suggestion Signed-off-by: Juan Lopez Fernandez --- thirdparty/mcap/mcap/reader.inl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thirdparty/mcap/mcap/reader.inl b/thirdparty/mcap/mcap/reader.inl index 5ca986604..5b51f9266 100644 --- a/thirdparty/mcap/mcap/reader.inl +++ b/thirdparty/mcap/mcap/reader.inl @@ -515,7 +515,7 @@ Status McapReader::readSummaryFromScan_(IReadable& reader) { 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, attachment_copy); + attachments_.emplace(attachment_copy.name, std::move(attachment_copy)); }; typedReader.onMetadata = [&](const Metadata& metadata, ByteOffset fileOffset) { MetadataIndex metadataIndex{metadata, fileOffset};