T22211: Potential crash in statistics calculator #171
Closed
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.
Problem is in smart pointers in calculator's members and in thrown exception.
Calculator contains two smart pointers to images: m_Image and m_InternalImage that is actually points to some slice from m_Image.
m_InternalImage is initialized in ImageStatisticsCalculator::ComputeStatistics() (inside ExtractImageAndMask()) and is cleared at the end of this function.
But if there is an exception in ExtractImageAndMask() (for example, if we calculate statistics for planar figure that is outside of image's boundaries), m_InternalImage isn't cleared at the end and still points to some slice. So far so good.
Later, when you set up new image to calculator, old image from m_Image is deleted but its ImageData isn't because m_InternalImage data holds it as parent. And of course m_InternalImage still exists at this point.
Later, ExtractImageAndMask() is called, m_InternalImage is rewritten, old image from it is deleted, image data from old m_Image is also deleted and deletes its own ImageVtkWriteAccessor. Which tries to access old m_Image that was deleted some time ago. And there is a crash.
In this PR I've moved m_InternalImage from member to local variable, so it will be released even in case of exception.
Signed-off-by: KuznetsovAlexander KuznetsovAlexander@rambler.ru