Search code examples
c++memory-corruptionsimd-library

Strange behavior of function Simd::Fill()


I use function Simd::Fill from Simd Library. And it works fine in the most cases. But there is a strange behavior sometimes. For example, in this case:

#include "Simd/SimdLib.hpp"

typedef Simd::View<Simd::Allocator> View;

View Create(size_t w, size_t h, uint8_t color)
{
    View image(w, h, View::Gray8);
    Simd::Fill(image, color);
    return image;
}

int main()
{
    View image;

    image = Create(200, 150, 127);
    image.Save("image.pgm");

    return 0;   
}

Saved "image.pgm":

enter image description here

There is a strange noise in the image. It is similar to memory corruption, but I can't find where is it. Could anybody help me?


Solution

  • It seems that I found a mistake which leads to this behavior. Assignment operator View& operator=(const View &view) of Simd::View class assigns only reference to other image. So your function Create() returns reference to local object.

    In order to create copy of the Simd::View you have to use function Simd::Copy() or method Simd::View::Clone().