Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: segfault when copying pixels  (Read 1567 times)

0 Members and 1 Guest are viewing this topic.

Mörkö

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
segfault when copying pixels
« on: June 28, 2019, 08:32:38 pm »
In the following code, the resulting texture is garbled when drawn:

    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);

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)

What am I doing wrong?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: segfault when copying pixels
« Reply #1 on: June 28, 2019, 10:19:52 pm »
Each pixel is more that just one byte. Each is, in fact, 4 bytes.

See documentation for getPixelsPtr():
https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Image.php#ad9562b126fc8d5efcf608166992865c7
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*