Error: "free pointer was not allocated" in c

this error always gets thrown when I try to free the allocated struct a second time, which it shouldn't because it is set to NULL after I free it.

here's my structure with no real pointer inside it:

 typedef struct{
        int frame; 
        double timestamp; 
        int identifier; 
        int state; 
        int unknown1; 
        int unknown2; 
        mtVector normalized;
        float size; 
        int unknown3;
        float angle; 
        float majorAxis;
        float minorAxis;
        mtVector unknown4; 
        int unknown5[2]; 
        float unknown6; 
    }Touch;

      

main barebone function:

int main(){
    Touch *myTouch = NULL;
    int inputCounter = 0;
    //whenever  a touch is recognized:
    ...
    myTouch = (Touch*)realloc(myTouch,sizeof(Touch)*(inputCounter++));
    ...
    // everything works fine until:
    freeTouch(myTouch);
}

void freeTouch(Touch *f){
    if(f != NULL){
        free(f);
        f = NULL;
    }
}

      

Anyone got an idea?

+3


source to share


3 answers


f

- local variable. free(f)

It will affect the allocated memory, but f = NULL

does not affect myTouch

in freeTouch(myTouch);

.

Try



void freeTouch(Touch **f){
    if(*f != NULL){
        free(*f);
        *f = NULL;
    }
}

      

and use freeTouch(&myTouch)

.

+3


source


You have two problems. The first is that it is not practical to explicitly specify the return value from malloc

or realloc

. This can cause problems if you forget to include the prototype / header in it.

Second, the deallocation f

within the function releases the local copy. Until C gets the links, there are two possibilities. First pass pointer to pointer and use this:

void freeTouch (Touch **pF){
    if (*pF != NULL){
        free (*pF);
        *pF = NULL;
    }
}
:
freeTouch (&myTouch);

      



or run NULL so you can assign:

void *freeTouch (Touch *f){
    free (f);
    return NULL;
}
:
myTouch = freeTouch (myTouch);

      

You will notice that the second doesn't care if you pass into NULL - it is perfectly acceptable to try to free the NULL pointer as it does not effectively work (other than the function call).

+2


source


First of all, never use

x = realloc(x, size);

      

because if it x

is allocated before and realloc

does not work, you do it NULL

while the memory still exists, and therefore you create garbage.

Secondly,

void freeTouch(Touch *f);

      

receives a pointer by value and therefore cannot modify the pointer itself. So yours is f = NULL;

not efficient. You need to change your code to:

int main(){
    Touch *myTouch = NULL, temp;
    int inputCounter = 0;
    //whenever  a touch is recognized:
    ...
    temp = realloc(myTouch,sizeof(*temp) * (inputCounter++));
    if (temp == NULL)
        /* handle error */
    myTouch = temp;
    ...
    // everything works fine until:
    freeTouch(&myTouch);
}

void freeTouch(Touch **f){
    if(f != NULL && *f != NULL){
        free(*f);
        *f = NULL;
    }
}

      

Sidenote: Recommended to use realloc

(and similarly malloc

) as follows:

x = realloc(count * sizeof(*x));

      

It is not necessary to output the output or realloc

. It also sizeof(*x)

allows you to avoid repeating the type x

every time.

+1


source







All Articles