0

I try to initialize a texture (in opengl). If the block transmitted to glTexSubImage2D is allocated with malloc, the initialization works well. If I use aligned_alloc I get, most of the time, a segfault. Why ?

The minimal piece of code to reproduce the issue:

GLuint texture_id;
GLenum err;

int sx = 2048;
int sy = 1024;
char *data = (char *)aligned_alloc(sizeof(char)*sx*sy*3, ALIGN_VALUE);
//char *data = (char *)malloc(sizeof(char)*sx*sy*3);

glGenTextures(1, &texture_id);
glActiveTexture(texture_unit);
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB8, sx, sy);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, sx, sy, GL_BGR, GL_UNSIGNED_BYTE, data);

Valgrind lists a lot of errors with aligned_alloc. Two (like the first one) on memalign at the position of the aligned_alloc invokation. All other (many like the second one) at the position of glTexSubImage2D command.

==7042== Invalid read of size 1
==7042==    at 0x4045F71: ??? (in /tmp/.glamhkos (deleted))
==7042==    by 0xCFFFFFF: ???
==7042==    by 0xD68A04F: ???
==7042==    by 0xFFEFFFE3F: ???
==7042==  Address 0xd000002 is 0 bytes after a block of size 2 alloc'd
==7042==    at 0x4C2B1B6: memalign (vg_replace_malloc.c:760)
...
==7042==    by 0x4016E1: main (in /.../)


==7042== Invalid read of size 1
==7042==    at 0x4045F71: ??? (in /tmp/.glamhkos (deleted))
==7042==    by 0xD0017FF: ???
==7042==    by 0xD68C04F: ???
==7042==    by 0xFFEFFFE3F: ???
==7042==    by 0x1: ???
==7042==    by 0x7B23325: ??? (in /usr/lib/x86_64-linux-gnu/libnvidia-glcore.so.352.79)
==7042==    by 0x7B2D9A6: ??? (in /usr/lib/x86_64-linux-gnu/libnvidia-glcore.so.352.79)
==7042==    by 0x7C7DD40: ??? (in /usr/lib/x86_64-linux-gnu/libnvidia-glcore.so.352.79)
==7042==    by 0x7C51383: ??? (in /usr/lib/x86_64-linux-gnu/libnvidia-glcore.so.352.79)
==7042==    by 0x7C52A16: ??? (in /usr/lib/x86_64-linux-gnu/libnvidia-glcore.so.352.79)
==7042==    by 0x7C6F553: ??? (in /usr/lib/x86_64-linux-gnu/libnvidia-glcore.so.352.79)
...
==7042==  Address 0xd001802 is 6,146 bytes inside a block of size 6,852,752 in arena "client"

Changing ALIGN_VALUE does not help.

Valgrind lists no error when I use malloc.

J.F.
  • 60
  • 6

1 Answers1

0

I wrongly use aligned_alloc(sizeof(char)sxsy*3, ALIGN_VALUE) in my minimal exemple. Parameters are wrong.

Use instead : aligned_alloc(ALIGN_VALUE, sizeof(char)sxsy*3)

J.F.
  • 60
  • 6