简体   繁体   中英

C: pointer being freed was not allocated — when calling free

My function:

Shot *shot_collide(Shot *shot)
{
    Shot *moving, *remaining;
    remaining=NULL;

    moving=shot;
    while(moving!=NULL)
    {
        if(moving->y>658) //ha eléri az alját
        {
            if(remaining==NULL)  //ha ez az első elem
            {
                shot=moving->next;
                free(moving);
                moving=shot->next;
            }else{

            remaining->next=moving->next;
            free(moving);
            moving=remaining->next;
            }
        }else{
            moving=moving->next;
        }

    }

    return shot;
}

When I call free, i get this error in Xcode:

NHF(1670,0x7fff77e1a300) malloc: * error for object 0x10050c9e0: pointer being freed was >not allocated * set a breakpoint in malloc_error_break to debug

This is an SDL game, and this function is free the shot after it reaches the end of the map.

I gotta finish this to Sunday, but I'm stuck :(

You have a bug.

The value you pass to free() must be a value returned by malloc() , or one of the related functions.

I see you are modifying your moving variable, but I don't think I have enough context to tell you exactly what needs to be changed.

But the value you are passing to free() is apparently not the value returned by malloc() .

EDIT 2

Look at lines 110 and 113 in your code (pastebin). You create moving, then you set moving = shot, then you free moving on line 121. You never used malloc() or calloc() on it in that translation block. That is why you are getting your error. Don't free it in that translation block. You probably need to free shot in the calling function (or wherever it was given memory.)

EDIT
If in your code, you created moving , then you changed moving to point to a different location than was returned originally, the problem would have been caused for a different reason. See illustration:

char *string = {0};
string = calloc(10, 1);//see 1)
string++;//see 2)
free(string);//see 3)  

1) string may look like this in memory:

  0x000BCA00
 >|< (pointer position returned from calloc)
  |0|0|0|0|0|0|0|0|0|0|  

2) pointer string is moved from its original position:

  0x000BCA04
         >|< (new ponter position)
  |0|0|0|0|0|0|0|0|0|0|8|  

3) 0x000BCA00 != 0x000BCA04, therefore free() will not work

End EDIT

If you compile and run:

int main(void)
{

    char *p = malloc(10);
    p++;  //pointer is no longer what malloc returned, so free will not work
    free(p);fails here
    return 0;
}  

You are likely to get the error message you described.
I get this one:

在此处输入图片说明

Look through your code to identify similar conditions. Better yet, follow your compiler error message's directions: set a breakpoint in malloc_error_break to debug

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM