简体   繁体   English

C:未分配正在释放的指针—调用free时

[英]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: 当我打电话免费时,我在Xcode中收到此错误:

NHF(1670,0x7fff77e1a300) malloc: * error for object 0x10050c9e0: pointer being freed was >not allocated * set a breakpoint in malloc_error_break to debug NHF(1670,0x7fff77e1a300)malloc: *对象0x10050c9e0的错误:指针释放>>未分配*在malloc_error_break中设置一个断点进行调试

This is an SDL game, and this function is free the shot after it reaches the end of the map. 这是一个SDL游戏,此功能在到达地图末端后即可释放。

I gotta finish this to Sunday, but I'm stuck :( 我要到星期天才能完成,但是我被卡住了:(

You have a bug. 你有个bug。

The value you pass to free() must be a value returned by malloc() , or one of the related functions. 传递给free()的值必须是malloc()或相关函数之一返回的值。

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. 我看到您正在修改moving变量,但我认为我没有足够的上下文来告诉您确切需要更改的内容。

But the value you are passing to free() is apparently not the value returned by malloc() . 但是,您传递给free()的值显然不是malloc()返回的值。

EDIT 2 编辑2

Look at lines 110 and 113 in your code (pastebin). 查看代码中的第110行和第113行(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. 创建移动,然后设置移动=射击,然后在第121行自由移动。在该转换块中,从未使用过malloc()calloc() 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. 如果在代码中创建了moving ,则更改了moving使其指向的位置与原始返回的位置不同,则可能是由于不同的原因引起的。 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: 1)字符串在内存中可能看起来像这样:

  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: 2)指针string从其原始位置移动:

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

3) 0x000BCA00 != 0x000BCA04, therefore free() will not work 3) 0x000BCA00!= 0x000BCA04,因此free()将不起作用

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 更好的是,遵循编译器错误消息的指示: 在malloc_error_break中设置一个断点以进行调试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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