COMP: Replace raw pointers with std::unique_ptr in VoxBoCUBImageIO#5997
Conversation
|
Thanks for splitting #5993, Hans! Can you please remove the first two commits from this PR? |
|
@hjmjohnson Please note that VoxBoCUBImageIO is in the "Nonunit/Review" module, so it may not be tested directly by the CI. Did you test it locally? |
Yes. I explicitly required a build and test in my local environment. |
7dd611e to
eb58cc8
Compare
- Change m_Reader/m_Writer from GenericCUBFileAdaptor* to
std::unique_ptr<GenericCUBFileAdaptor>
- Change CreateReader/CreateWriter return types to std::unique_ptr;
use std::make_unique for polymorphic factory creation
- Call m_Writer.reset() at end of Write() to preserve original
close-on-write behavior (flush/close file immediately)
- Remove {} brace-initializers from unique_ptr members: GCC 7 has a
known bug (fixed in GCC 9.2) where brace-initializing a smart pointer
to a forward-declared class instantiates ~unique_ptr<T> with an
incomplete type. See ITK#3877.
- Declare ~VoxBoCUBImageIO() in the header and define it as = default
in the .cxx file where GenericCUBFileAdaptor is a complete type.
Defining = default in the header would instantiate
~unique_ptr<GenericCUBFileAdaptor> with an incomplete type, causing
undefined behavior. See https://andreasfertig.com/blog/2023/12/when-an-empty-destructor-is-required/
- Add <memory> include
eb58cc8 to
6c699a6
Compare
|
| Filename | Overview |
|---|---|
| Modules/Nonunit/Review/include/itkVoxBoCUBImageIO.h | Adds <memory>, changes m_Reader/m_Writer to std::unique_ptr, updates CreateReader/CreateWriter signatures; destructor kept declared but deferred to .cxx — all correct. |
| Modules/Nonunit/Review/src/itkVoxBoCUBImageIO.cxx | Destructor correctly defined as = default in .cxx (complete-type context), std::make_unique replaces new, all manual delete calls removed, m_Writer.reset() preserves close-on-write semantics — clean and correct. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[VoxBoCUBImageIO created] --> B[m_Reader = nullptr
m_Writer = nullptr
via unique_ptr default init]
B --> C{CanReadFile called}
C --> D[local reader = CreateReader
scoped unique_ptr]
D --> E{reader == nullptr?}
E -- yes --> F[return false]
E -- no --> G[check header content]
G --> H[reader auto-destroyed
on scope exit]
H --> I[return iscub]
B --> J{ReadImageInformation called}
J --> K[m_Reader = CreateReader
releases old reader]
K --> L{m_Reader == nullptr?}
L -- yes --> M[throw exception]
L -- no --> N[read header & parse metadata
m_Reader kept alive]
B --> O{Write called}
O --> P[m_Writer = CreateWriter
releases any old writer]
P --> Q[WriteImageInformation
uses m_Writer]
Q --> R[m_Writer->WriteData]
R --> S[m_Writer.reset
flushes & closes file]
B --> T[VoxBoCUBImageIO destroyed]
T --> U[~unique_ptr destroys
m_Reader and m_Writer
if still set]
Reviews (1): Last reviewed commit: "COMP: Replace raw pointers with std::uni..." | Re-trigger Greptile
Summary
Replace raw pointer ownership in
VoxBoCUBImageIOwithstd::unique_ptr, following C++ Core Guidelines R.11 and R.20.m_Readerandm_Writermembers fromGenericCUBFileAdaptor*tostd::unique_ptr<GenericCUBFileAdaptor>CreateReader/CreateWriterreturn types tostd::unique_ptr<GenericCUBFileAdaptor>; usestd::make_uniquefor polymorphic factory creationdeleteneededm_Writer.reset()at end ofWrite()to preserve the original close-on-write behaviour (flush/close the file immediately after writing, matching the originaldelete m_Writercall){}brace-initializers from theunique_ptrmembers to avoid a GCC 7 bug with brace-initialization of smart pointers to forward-declared classes (reported in COMP: Remove in-class {} member initializers of unique_ptr #3877)<memory>includeTest plan
ctest -R VoxBo)🤖 Generated with Claude Code