@@ -351,6 +351,7 @@ void McapReader::reset_() {
351
351
statistics_ = std::nullopt;
352
352
chunkIndexes_.clear ();
353
353
attachmentIndexes_.clear ();
354
+ clear_attachments_ ();
354
355
schemas_.clear ();
355
356
channels_.clear ();
356
357
dataStart_ = 0 ;
@@ -360,6 +361,13 @@ void McapReader::reset_() {
360
361
parsedSummary_ = false ;
361
362
}
362
363
364
+ void McapReader::clear_attachments_ () {
365
+ for (auto & [attachment_name, attachment] : attachments_) {
366
+ std::free ((void *)attachment.data );
367
+ }
368
+ attachments_.clear ();
369
+ }
370
+
363
371
Status McapReader::readSummary (ReadSummaryMethod method, const ProblemCallback& onProblem) {
364
372
if (!input_) {
365
373
const Status status{StatusCode::NotOpen};
@@ -427,7 +435,7 @@ Status McapReader::readSummarySection_(IReadable& reader) {
427
435
}
428
436
429
437
attachmentIndexes_.clear ();
430
- attachments_. clear ();
438
+ clear_attachments_ ();
431
439
metadataIndexes_.clear ();
432
440
metadata_.clear ();
433
441
chunkIndexes_.clear ();
@@ -488,7 +496,7 @@ Status McapReader::readSummaryFromScan_(IReadable& reader) {
488
496
schemas_.clear ();
489
497
channels_.clear ();
490
498
attachmentIndexes_.clear ();
491
- attachments_. clear ();
499
+ clear_attachments_ ();
492
500
metadataIndexes_.clear ();
493
501
metadata_.clear ();
494
502
chunkIndexes_.clear ();
@@ -503,7 +511,11 @@ Status McapReader::readSummaryFromScan_(IReadable& reader) {
503
511
typedReader.onAttachment = [&](const Attachment& attachment, ByteOffset fileOffset) {
504
512
AttachmentIndex attachmentIndex{attachment, fileOffset};
505
513
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));
507
519
};
508
520
typedReader.onMetadata = [&](const Metadata& metadata, ByteOffset fileOffset) {
509
521
MetadataIndex metadataIndex{metadata, fileOffset};
0 commit comments