Skip to content

Commit 70097cc

Browse files
Copy attachments buffer when parsing summary (#100)
* 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>
1 parent 06d37fe commit 70097cc

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

thirdparty/mcap/mcap/reader.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ class MCAP_PUBLIC McapReader final {
490490
bool parsedSummary_ = false;
491491

492492
void reset_();
493+
void clear_attachments_();
493494
Status readSummarySection_(IReadable& reader);
494495
Status readSummaryFromScan_(IReadable& reader);
495496
};

thirdparty/mcap/mcap/reader.inl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ void McapReader::reset_() {
351351
statistics_ = std::nullopt;
352352
chunkIndexes_.clear();
353353
attachmentIndexes_.clear();
354+
clear_attachments_();
354355
schemas_.clear();
355356
channels_.clear();
356357
dataStart_ = 0;
@@ -360,6 +361,13 @@ void McapReader::reset_() {
360361
parsedSummary_ = false;
361362
}
362363

364+
void McapReader::clear_attachments_() {
365+
for (auto& [attachment_name, attachment] : attachments_) {
366+
std::free((void*)attachment.data);
367+
}
368+
attachments_.clear();
369+
}
370+
363371
Status McapReader::readSummary(ReadSummaryMethod method, const ProblemCallback& onProblem) {
364372
if (!input_) {
365373
const Status status{StatusCode::NotOpen};
@@ -427,7 +435,7 @@ Status McapReader::readSummarySection_(IReadable& reader) {
427435
}
428436

429437
attachmentIndexes_.clear();
430-
attachments_.clear();
438+
clear_attachments_();
431439
metadataIndexes_.clear();
432440
metadata_.clear();
433441
chunkIndexes_.clear();
@@ -488,7 +496,7 @@ Status McapReader::readSummaryFromScan_(IReadable& reader) {
488496
schemas_.clear();
489497
channels_.clear();
490498
attachmentIndexes_.clear();
491-
attachments_.clear();
499+
clear_attachments_();
492500
metadataIndexes_.clear();
493501
metadata_.clear();
494502
chunkIndexes_.clear();
@@ -503,7 +511,11 @@ Status McapReader::readSummaryFromScan_(IReadable& reader) {
503511
typedReader.onAttachment = [&](const Attachment& attachment, ByteOffset fileOffset) {
504512
AttachmentIndex attachmentIndex{attachment, fileOffset};
505513
attachmentIndexes_.emplace(attachment.name, attachmentIndex);
506-
attachments_.emplace(attachment.name, attachment);
514+
// Copy buffer for storage as original is freed when leaving scope
515+
Attachment attachment_copy = attachment;
516+
attachment_copy.data = (std::byte*)std::malloc(attachment.dataSize);
517+
std::memcpy((void*)attachment_copy.data, attachment.data, attachment.dataSize);
518+
attachments_.emplace(attachment_copy.name, std::move(attachment_copy));
507519
};
508520
typedReader.onMetadata = [&](const Metadata& metadata, ByteOffset fileOffset) {
509521
MetadataIndex metadataIndex{metadata, fileOffset};

0 commit comments

Comments
 (0)