0
votes

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 :(

2
you moved the pointer between the time it was created and when you attempted to free it. You are not showing enough code to allow a test compile and run, so difficult to know for sure. But if you do something like char *p = malloc(10);, then... p++;, then... free(p); you will likely get the error you described. - ryyker
What line in the pastebin are you getting the error you described? ( pointer being freed was >not allocated * set a breakpoint in malloc_error_break to debug ) Have you followed the messages instruction? - ryyker
121. the first free in function, what i wrote in the question - fzsombor
I tried. I get this: i.imgur.com/elfSY8X.png - fzsombor
My answer below did not exactly address your specific issue. I mis-stated the real issue I think. Your real issue is simply that within the translation unit (function) you saw the error message, moving was created, assigned a value, then attempted to be freed without ever having calloc or malloc called to assign memory. That simply is the reason for your error message I think. Sorry for the confusion before. - ryyker

2 Answers

0
votes

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().

0
votes

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:

enter image description here

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