1
Graphics / segfault when copying pixels
« on: June 28, 2019, 08:32:38 pm »
In the following code, the resulting texture is garbled when drawn:
but using the orignal pixels array is no problem. Valgrind reports the following:
What am I doing wrong?
sf::Image img = texture.copyToImage();
const sf::Vector2u img_size = texture.getSize();
const sf::Uint8 * pixels = img.getPixelsPtr();
const std::size_t array_size = img_size.x * img_size.y;
sf::Uint8 * _pixels = new sf::Uint8[array_size];
for (std::size_t i = 0; i < array_size; i++) {
_pixels[i] = pixels[i];
}
texture.update(_pixels, 256, 256, 0, 0);
const sf::Vector2u img_size = texture.getSize();
const sf::Uint8 * pixels = img.getPixelsPtr();
const std::size_t array_size = img_size.x * img_size.y;
sf::Uint8 * _pixels = new sf::Uint8[array_size];
for (std::size_t i = 0; i < array_size; i++) {
_pixels[i] = pixels[i];
}
texture.update(_pixels, 256, 256, 0, 0);
but using the orignal pixels array is no problem. Valgrind reports the following:
==16020== Invalid read of size 8
==16020== at 0x4C3416E: memcpy@GLIBC_2.2.5 (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==16020== by 0xB98CDA2: ??? (in /usr/lib/x86_64-linux-gnu/dri/r600_dri.so)
==16020== by 0xB991D90: ??? (in /usr/lib/x86_64-linux-gnu/dri/r600_dri.so)
==16020== by 0xB6E91D4: ??? (in /usr/lib/x86_64-linux-gnu/dri/r600_dri.so)
==16020== by 0xB6673B4: ??? (in /usr/lib/x86_64-linux-gnu/dri/r600_dri.so)
==16020== by 0xB669F5F: ??? (in /usr/lib/x86_64-linux-gnu/dri/r600_dri.so)
==16020== by 0xB66D3C7: ??? (in /usr/lib/x86_64-linux-gnu/dri/r600_dri.so)
==16020== by 0x5294FCF: sf::Texture::update(unsigned char const*, unsigned int, unsigned int, unsigned int, unsigned int) (in /usr/lib/x86_64-linux-gnu/libsfml-graphics.so.2.4.2)
==16020== at 0x4C3416E: memcpy@GLIBC_2.2.5 (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==16020== by 0xB98CDA2: ??? (in /usr/lib/x86_64-linux-gnu/dri/r600_dri.so)
==16020== by 0xB991D90: ??? (in /usr/lib/x86_64-linux-gnu/dri/r600_dri.so)
==16020== by 0xB6E91D4: ??? (in /usr/lib/x86_64-linux-gnu/dri/r600_dri.so)
==16020== by 0xB6673B4: ??? (in /usr/lib/x86_64-linux-gnu/dri/r600_dri.so)
==16020== by 0xB669F5F: ??? (in /usr/lib/x86_64-linux-gnu/dri/r600_dri.so)
==16020== by 0xB66D3C7: ??? (in /usr/lib/x86_64-linux-gnu/dri/r600_dri.so)
==16020== by 0x5294FCF: sf::Texture::update(unsigned char const*, unsigned int, unsigned int, unsigned int, unsigned int) (in /usr/lib/x86_64-linux-gnu/libsfml-graphics.so.2.4.2)
What am I doing wrong?