Skip to content

BUG: Call non-const GetBufferPointer() in non-const image iterators - #6955

Merged
hjmjohnson merged 1 commit into
InsightSoftwareConsortium:mainfrom
SimonRit:Iterator_GetBufferPointer
Oct 21, 2026
Merged

hjmjohnson merged 1 commit into
InsightSoftwareConsortium:mainfrom
SimonRit:Iterator_GetBufferPointer

Conversation

@SimonRit

Copy link
Copy Markdown

The const base class constructors only call the const overload, so image types relying on side effects of the non-const overload (e.g. CudaImage marking its GPU buffer dirty) were not notified of writes.

Closes #6934. Based on the discussions in RTKConsortium/ITKCudaCommon#107. I'm (of course) open to alternative suggestions for the implementation!

PR Checklist

  • No API changes were made (or the changes have been approved)
  • No major design changes were made (or the changes have been approved)
  • Added test (or behavior not changed)
  • Updated API documentation (or API not changed)
  • Added license to new files (if any)
  • Added Python wrapping to new files (if any) as described in ITK Software Guide Section 9.5
  • Added ITK examples for all new major features (if any)

Refer to the ITK Software Guide for
further development details if necessary.

@github-actions github-actions Bot added type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Core Issues affecting the Core module area:Filtering Issues affecting the Filtering module labels Oct 20, 2026
@SimonRit

Copy link
Copy Markdown
Author

cc @mymonin

@greptile-apps

greptile-apps Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5 Tier: plus

[Medium impact] The change reaches writable iterators across several ITK image workflows.

Complete the public reverse-iterator conversion fix before merging so GPU callers do not miss pixel edits.

Findings

  1. P1 GPU misses reverse-iterator edits ▶

Summary

Writable iterator constructors now call non-const GetBufferPointer().

  • Writable image iterators tell images when they request writable buffers.

Reviews (1) · Last reviewed commit: "BUG: Call non-const GetBufferPointer() i..." · Reviewed by Greptile

Comment on lines +28 to +29
// Base class calls the const GetBufferPointer(); call the non-const one for its side effects (e.g. CudaImage).
this->m_Buffer = ptr->GetBufferPointer();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 GPU misses reverse-iterator edits

The public ImageRegionReverseIterator(const ImageConstIterator<TImage> &) constructor still skips the non-const GetBufferPointer() call. When a caller converts a read-only iterator over a CudaImage with a clean GPU buffer, then writes through Set(), this path never marks the GPU buffer dirty. Later GPU work can therefore miss those writes.

Apply the same notification to that constructor and add a conversion case to the counting-image test.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed by latest force push 05979da

@SimonRit
SimonRit force-pushed the Iterator_GetBufferPointer branch from aa46331 to 05979da Compare October 20, 2026 10:06
The const base class constructors only call the const overload, so
image types relying on side effects of the non-const overload (e.g.
CudaImage marking its GPU buffer dirty) were not notified of writes.

Closes InsightSoftwareConsortium#6934.
@SimonRit
SimonRit force-pushed the Iterator_GetBufferPointer branch from 05979da to 9a6c182 Compare October 20, 2026 11:56
@mymonin

mymonin commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Looks good to me, assuming the proposed changes make the new tests pass. I was waiting for some acknowledgement of the issue I submitted but your PR makes everything clearer.

@SimonRit
SimonRit force-pushed the Iterator_GetBufferPointer branch from 9a6c182 to 5db0ef9 Compare October 20, 2026 13:44
@N-Dekker
N-Dekker self-requested a review October 20, 2026 13:46
{}
{
// Source iterator holds a const image; call the non-const GetBufferPointer() for its side effects (e.g. CudaImage).
(void)static_cast<void *>(const_cast<TImage *>(this->m_Image.GetPointer())->GetBufferPointer());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit suspicious, are you sure it's OK? I mean, casting away the const, and then (effectively) setting the dirty flag of the const image looks risky!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, this is what is currently done: all images, whether const or not, are set dirty:
https://github.com/RTKConsortium/ITKCudaCommon/blob/main/include/itkCudaImage.hxx#L185-L191
This is done to circumvent the issue this PR aims at addressing. Since I'm trying to fix this issue :-), you could question why I did not try to fix this too in this PR? I was convinced by Claude when I suggested to make this constructor protected:

"Why it's public: it dates from the original reverse-iterator commit (38982a2, 2001). Its job is to turn any forward iterator into a reverse one. The parameter has to be ImageConstIterator because that's the only common base. ImageRegionIterator, the usual writable iterator, doesn't derive from ImageIterator; it derives from ImageRegionConstIterator. The repo uses it exactly this way: itkImageReverseIteratorTest.cxx:179 builds an ImageRegionReverseIterator from an ImageRegionIterator.

Why it's questionable: the same parameter type also accepts a real const iterator over a const image. You then get a writable iterator with no const_cast in sight. Every other const-to-writable conversion in ITK is protected "in order to enforce const correctness", including this class's other one: ImageRegionReverseIterator(const ImageRegionReverseConstIterator &).

Why not just make it protected: that breaks the existing test, and any outside code that converts a forward iterator to a reverse one. Doing it properly would need a deprecation and a replacement, such as overloads that take the writable forward iterator types. That's an API change, out of scope for this bug fix.

My recommendation: leave it public"

@N-Dekker
N-Dekker self-requested a review October 20, 2026 13:59

@N-Dekker N-Dekker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Simon!

I don't really like the idea that GetBufferPointer() could be overridden to set the "dirty" flag of a derived image, and I don't really like const and non-const overloads to behave so differently, but I see that this PR should do the job, and make CudaImage users happy 🤷

Excellent unit tests 💯 by the way!

{}
{
// Base class calls the const GetBufferPointer(); call the non-const one for its side effects (e.g. CudaImage).
(void)static_cast<void *>(ptr->GetBufferPointer());

@N-Dekker N-Dekker Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops:

  itkImageRegionIteratorWithIndex.hxx:29:9: error: invalid 'static_cast' from type 'const itk::CovariantVector&lt
  double, 2&gt

at https://github.com/InsightSoftwareConsortium/ITK/actions/runs/39433022238/job/118662998617#step:13:102

Does that mean that non-const ImageRegionIteratorWithIndex is actually used to iterate over const images? If so, it might be fixed by adding a non-const-check:

    if ( ! std::is_const<TImage> )
    {
      (void)static_cast<void *>(ptr->GetBufferPointer());
    }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I would suggest users to use ImageRegionConstIteratorWithIndex instead, for const images 🤷

@mymonin mymonin Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there ever a valid reason to create a mutable iterator over a const image ? I would assume a const image only exposes a const pointer to its buffer, which would deliberately not be suitable to construct a writable iterator... but if so, I think the check should be if constexpr (! std::is_const_v<std::remove_reference_t<TImage>>)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I'm following. Can you explain why std::remove_reference_t is required here please?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake, I saw the error containing a reference so I reflexively assumed it should be removed but since we pass a TImage* , there is no risk for TImage being deduced as a reference type.

@SimonRit SimonRit Oct 21, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am doubtful on the solution here. I have the impression my attempt to fix #6934 has uncovered another issue. I see two options:

  • Rollback to
static_cast<void>(ptr->GetBufferPointer());

or add the suggested condition (this is equivalent in my opinion) and ignore the underlying issue.

  • Leave it as is, fix itkKappaStatisticImageToImageMetricTest.cxx by using an itk::ImageRegionConstIteratorWithIndex, but this will likely break some other codes which use non const iterators even if they just use const functionalities.

The latter point might be annoying so we might want to put this between some backward compatibilty guards.
What do you think @N-Dekker?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well... I did not realize that static_cast<void *>(ptr->GetBufferPointer()) would cause any compilation errors, although it is useful that it detected such unintended iterator uses. But then I don't really like to have those if-not-const-Image statements duplicated all over the place. A proper solution might be to add a helper function, like CallGetBufferPointerWhenImageIsNonConst(image). But then, I don't know where to place that helper function in the source tree.

So for pragmatism reasons, I'm now considering static_cast<void>(ptr->GetBufferPointer()) acceptable. It's a compromise anyway 🤷

Thanks!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your input, I think we agree :-). I reverted to 9a6c182 which did that.

@hjmjohnson

Copy link
Copy Markdown
Member

/azp run ITK.Linux.Python,ITK.macOS.Python

@hjmjohnson

Copy link
Copy Markdown
Member

/azp run ITK.Linux.Python

@hjmjohnson

Copy link
Copy Markdown
Member

/azp run ITK.macOS.Python

@hjmjohnson
hjmjohnson merged commit 814e4b3 into InsightSoftwareConsortium:main Oct 21, 2026
31 of 37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Core Issues affecting the Core module area:Filtering Issues affecting the Filtering module type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct

Projects

None yet

4 participants