Why is this not working?

I'v tried to solve a memory leak in the GLU callback by creating a global variable, but now it does nothing:

GLdouble *gluptr = NULL;

void CALLBACK combineCallback(GLdouble coords[3], GLdouble *vertex_data[4],
                              GLfloat weight[4], GLdouble **dataOut)
{
    GLdouble *vertex;
    if(gluptr == NULL)
    {
        gluptr = (GLdouble *) malloc(6 * sizeof(GLdouble));
    }
    vertex = (GLdouble*)gluptr;
    vertex[0] = coords[0];
    vertex[1] = coords[1];
    vertex[2] = coords[2];


    for (int i = 3; i < 6; i++)
    {

        vertex[i] = weight[0] * vertex_data[0][i] +
            weight[1] * vertex_data[0][i] +
            weight[2] * vertex_data[0][i] +
            weight[3] * vertex_data[0][i];
    }


    *dataOut = vertex;


}

basically instead of doing malloc every time in the loop (thus, a memory leak) using a global pointer, but this does not work (drawing on the screen does not work), which means that dataOut does not receive the vertex data pointed to by to my pointer. Why does using malloc for a pointer created in this function work differently than a global variable?

thank

+3
source share
3 answers

You only select data once, but GLUtesselator requires more than one data set at a time!

, , . GLUtesselator .

do

void gluDeleteTess(GLUtesselator *tessobj);

... , ?

+2

, , - combCallback(), combCallback() clobber .

+1

, , , " ", dataOut, -, malloc, , WILL , , , -, , vertex gluptr - . , gluptr vertex coords 'GLDouble', vertex ... , , dataOut... , , ...

, , , NULL...

GLdouble *gluptr = NULL;

void CALLBACK combineCallback(GLdouble coords[3], GLdouble *vertex_data[4],
                              GLfloat weight[4], GLdouble **dataOut)
{
    if((*dataOut) == NULL)
    {
        (*dataOut) = (GLdouble *) malloc(6 * sizeof(GLdouble));
    }
    if (*dataOut != NULL){
       /* PASSED MEMORY ALLOC! */
       (*dataOut)[0] = coords[0];
       (*dataOut)[1] = coords[1];
       (*dataOut)[2] = coords[2];

       for (int i = 3; i < 6; i++)
       {

        (*dataOut)[i] = weight[0] * vertex_data[0][i] +
            weight[1] * vertex_data[0][i] +
            weight[2] * vertex_data[0][i] +
            weight[3] * vertex_data[0][i];
       }
    }
}

The last parameter when calling this function combineCallbackis the parameter for calling by reference, therefore, a double asterisk is used.

Should I ask this, dataOutspecifically a fixed size of 6 elements? if so, then the parameter will need to be adjusted ... so that it looks like *(*dataOut[6])... looking at it from above my head (it's late and past my sleep ...)

0
source

Source: https://habr.com/ru/post/1746935/


All Articles