Return const references from member accessors#46
Merged
Conversation
Detector::panels() returned std::vector<Panel> by value. Binding a reference to panels()[0] bound to a subobject of that temporary, which is destroyed at the end of the statement, leaving the panel dangling for its whole lifetime. It compiled without warnings and failed only non-deterministically at runtime (observed downstream as corrupt Panel::get_origin() values). It also deep-copied the whole panel vector on every call. Change Detector::panels() to return const std::vector<Panel> &, and audit the remaining DX2 accessors for the same pattern. Crystal:: get_unit_cell(), Crystal::get_space_group() and ImageSequence:: filename() returned heap-owning members by value and are now const references too. Computed accessors and small trivially-copyable value types are intentionally left returning by value.
jbeilstenedmands
approved these changes
May 18, 2026
Contributor
jbeilstenedmands
left a comment
There was a problem hiding this comment.
Thanks for performing this audit 👍
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes
Detector::panels()returningstd::vector<Panel>byvalue, which created dangling references for callers binding to
panels()[0]and deep-copied the heavy panel vector on every call. Italso audits the remaining DX2 accessors so we don't hit the same silent,
non-error-throwing issue elsewhere.
Changes:
Detector::panels()(include/dx2/detector.hpp,dx2/detector.cxx) fromstd::vector<Panel>toconst std::vector<Panel> &, the idiomatic accessor form. This removes boththe dangling-reference issue and the per-call deep copy of every heavy
Panel.beam,crystal,goniometer,scan,experiment,imagesequenceandreflectionfor the same pattern.Three further member-backed accessors returned heap-owning types by
value and are now const references:
Crystal::get_unit_cell()now returnsconst gemmi::UnitCell &Crystal::get_space_group()now returnsconst gemmi::SpaceGroup &ImageSequence::filename()now returnsconst std::string &rationale:
Beam::get_s0(),Scan::get_oscillation(),etc.) construct a new value and must return by value.
fixed-size
Matrix3d/Vector3d,std::array<…,2>,double) arestack-only, cheap to copy, and carry no dangling-subobject risk in
practice.
ReflectionTablevector accessors andExperiment::identifier()/goniometer()already use theconst-reference form.
return of the changed accessors, so there were no call-site changes.
Existing
gemmi::UnitCell cell = crystal.get_unit_cell();copiesremain valid.
This removes a runtime-only, non-deterministic memory-corruption class
from the detector model and brings the rest of the DX2 accessors into a
consistent, audited state.
Closes #45.