copenglsegmentation-fault

Integer array seemingly deallocates itself when passed by reference to a function


I am trying to make an opengl program. I have made some files to abstract some of it's features. Here is my code:

VAO_t vao[2];

// First Polygon
vaoGen(&vao[0]);
vaoBind(&vao[0]);

int len1;
float *polygon1Verts = genPolygonVerts({ .x=0.5f, .y=0.5f, .radius=0.3f }, ids.ID1, &len1);
float *polygon1Colors = genPolygonColors(len1, 1.0f, 0, 0);

VBO_t *vbo1 = (VBO_t *)malloc(sizeof(VBO_t) * 2);
// VBO_t *vbo1 = new VBO_t[3];
vboGen(&vbo1[0], polygon1Verts, sizeof(float) * len1, GL_STATIC_DRAW);
vboGen(&vbo1[1], polygon1Colors, sizeof(float) * len1, GL_STATIC_DRAW);

VBLayout_t vbl1;
vbl_new(&vbl1, 3 * sizeof(float));
vbl_push_float(&vbl1, 3);
vbl_push_float(&vbl1, 3);

vaoAddBufferM(&vao[0], &vbo1, &vbl1);
printf("hello\n");
DBG_GLCHECKERROR();

My problem lies with the vbo1 array. I allocate it with malloc and then initialize its elements with vboGen function calls. VBO_t is typedefed uint. But when the vaoAddBufferM is run I get segmentation fault. Here is it's code:

void vaoAddBufferM(VAO_t *vao, VBO_t *vbo[], VBLayout_t *layout)
{
    vaoBind(vao);
    uint32_t offset = 0;
    for(uint32_t i = 0; i < layout->size; i++) {
        DBG_ASSERT(vbo[i] != NULL);
        vboBind(vbo[i]);
        glEnableVertexAttribArray(i);
        DBG_GLCHECKERROR();
        glVertexAttribPointer(
            i,
            layout->elements[i].count,
            layout->elements[i].type,
            layout->elements[i].normalized,
            layout->stride,
            (const void *)(size_t)offset
            );
        DBG_GLCHECKERROR();
        offset += layout->elements[i].count * layout->elements[i].typeSize;
    }
}

I tried debugging it with gdb. Before the function call the vbo1 array has first element 1 and second 2 which where initialized with glGenBuffer by the vboGen. But inside the vaoAdderBufferM the second element disappears and I can't access that memory with gdb, which causes the segmentation fault when vaoBind tries to read it. I am lost and don't know what could cause this behavior. I run valgrind to get more information and I get:

==2703110== 1 errors in context 1 of 3:
==2703110== Use of uninitialised value of size 8
==2703110==    at 0x10BF02: vboBind (vbo.c:6)
==2703110==    by 0x10BDC9: vaoAddBufferM (vao.c:44)
==2703110==    by 0x10A675: main (58633_11.cpp:74)
==2703110==  Uninitialised value was created by a stack allocation
==2703110==    at 0x10A3CE: main (58633_11.cpp:36)

I don't understand the message. Why does it says it was created by a stack allocation. Doesn't malloc allocate in the heap?


Solution

  • Since you're passing a pointer to vbo1, you need to dereference the pointer before indexing it in the function.

    But there's no need to pass the address of the variable in the first place. Just pass the pointer, and index it to get the elements.

        vaoAddBufferM(&vao[0], vbo1, &vbl1);
    
    void vaoAddBufferM(VAO_t *vao, VBO_t vbo[], VBLayout_t *layout)