Change IPeak python export to be shared_ptr instead of raw pointer#41876
Open
rosswhitfield wants to merge 2 commits into
Open
Change IPeak python export to be shared_ptr instead of raw pointer#41876rosswhitfield wants to merge 2 commits into
rosswhitfield wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the Mantid Python bindings for IPeaksWorkspace.getPeak() (and peak iteration) so that Python IPeak objects hold shared ownership of the originating workspace, preventing use-after-free crashes when the workspace is deleted from the ADS. This addresses the long-standing Python segmentation fault described in #31345.
Changes:
- Changed the
IPeaksWorkspace.getPeak()Python export to return an aliasingstd::shared_ptr<IPeak>that co-owns the workspace. - Updated the Python iterator for peaks workspaces to return the same shared-ownership peak handles.
- Added Python unit tests to verify peak usability after
DeleteWorkspaceand that peak modifications remain reflected in the workspace.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| Framework/PythonInterface/mantid/api/src/Exports/IPeaksWorkspace.cpp | Updates Boost.Python bindings to return peaks as aliasing shared_ptr handles and adjusts iteration to preserve workspace lifetime. |
| Framework/PythonInterface/mantid/api/src/Exports/IPeak.cpp | Registers std::shared_ptr<IPeak> for Boost.Python conversions to support the new return type. |
| Framework/PythonInterface/test/python/mantid/api/IPeaksWorkspaceTest.py | Adds regression tests covering post-deletion peak access and ensuring workspace-reflective peak mutation semantics. |
rosswhitfield
marked this pull request as ready for review
July 22, 2026 04:02
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.
Description of work
Fix segfault when accessing Peak from python after PeaksWorkspace has been deleted. This bug has existed in Mantid since at least Mantid 3.10.
By changing
IPeakto be ashared_ptrthe peaks workspace will exist as long a the pythonPeak's continue to exist. So this will fixes the segfault.Closes #31345
To test:
This causes a segfault that should now be fixed
Reviewer
Your comments will be used as part of the gatekeeper process. Comment clearly on what you have checked and tested during your review. Provide an audit trail for any changes requested.
As per the review guidelines:
mantid-developersormantid-contributorsteams, add a review commentrerun cito authorize/rerun the CIGatekeeper
As per the gatekeeping guidelines: