[Insight-developers] Interpolator failures on VS due to disabled return value optimization and VectorImage

Bradley Lowekamp blowekamp at mail.nih.gov
Wed May 29 15:07:24 EDT 2013


Hello,

How many people know about return value optimization? Its a very good thing to know about! Here a wiki page to know what it's about:

http://en.wikipedia.org/wiki/Return_value_optimization


The relevant part for this discussion: "an implementation may omit a copy operation resulting from a return statement, even if the copy constructor has side effects".

It appears that all the compiler still aggressively use return value optimization in debug mode except Visual Studio. It's calling the copy constructor when the is a local, and the tests are failing due to the side-effect occurring.


So The VectorImage::GetPixel method constructs a VariableLengthVector which doesn't owns the memory, but references and returns it:

https://github.com/Kitware/ITK/blob/master/Modules/Core/Common/include/itkVectorImage.h#L244

Then in the tests we are setting this returned value assuming it will modify the data in the actual Image:

https://github.com/Kitware/ITK/blob/master/Modules/Core/ImageFunction/test/itkLinearInterpolateImageFunctionTest.cxx#L146

It will do the modification if return value optimization has occurred. But if a copy constructor is called as it is with Visual Studio, then a deep copy of the VariableLengthVector will have occurred do the image will not get modified.

I thought this was a very interesting bug. And that sharing it may make more developers aware of this potential issue was writing VectorImage and VariableLengthVector code.

Here is the patch which should fix the outstanding Interpolator failing tests with Visual Studio debug:

http://review.source.kitware.com/#/c/11456/

Brad


More information about the Insight-developers mailing list