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 call free, error in xcode:
nhf(1670,0x7fff77e1a300) malloc: * error object 0x10050c9e0: pointer being freed >not allocated * set breakpoint in malloc_error_break debug
this sdl game, , function free shot after reaches end of map.
i gotta finish sunday, i'm stuck :(
edit 2
look @ lines 110 , 113 in code (pastebin). create moving, set moving = shot, free moving on line 121. never used malloc()
or calloc()
on in translation block. why getting error. don't free in translation block. need free shot in calling function (or wherever given memory.)
edit
if in code, created moving
, changed moving
point different location returned originally, problem have been caused different reason. see illustration:
char *string = {0}; string = calloc(10, 1);//see 1) string++;//see 2) free(string);//see 3)
1) string may in memory:
0x000bca00 >|< (pointer position returned calloc) |0|0|0|0|0|0|0|0|0|0|
2) pointer string
moved original position:
0x000bca04 >|< (new ponter position) |0|0|0|0|0|0|0|0|0|0|8|
3) 0x000bca00 != 0x000bca04, therefore free() not work
end edit
if compile , run:
int main(void) { char *p = malloc(10); p++; //pointer no longer malloc returned, free not work free(p);fails here return 0; }
you error message described.
one:
look through code identify similar conditions. better yet, follow compiler error message's directions: set breakpoint in malloc_error_break debug
Comments
Post a Comment