C free()double free或者腐败(全局数组)

时间:2013-04-29 15:05:41

标签: c malloc free

这是header-file中的结构:

typedef struct Missile {
    int id;
    Model *model;
    Point3D coords;
    int counter;
    float speed;
    float yaw;
    float pitch;
    float roll;
} Missile;

这是来自C文件的代码:

#define INIT_MISSILE_ARRAY_SIZE 4

/* An array to keep track of all missiles created (fired) */
static Missile *missiles[INIT_MISSILE_ARRAY_SIZE];

/* This keeps track of the array missiles' size */
int currentArraySize = INIT_MISSILE_ARRAY_SIZE;

int numOfMissiles = 0;

Missile *CreateMissile()
{
    if (numOfMissiles < currentArraySize)
    {
        Missile *missile = malloc(sizeof(Missile));
        missile->model = LoadModelPlus("rocket1.obj");
        missile->counter = 0;
        missile->speed = 0.00;
        missile->yaw = 0.00;
        missile->pitch = 0.00;
        missile->roll = 0.00;
        missile->id = numOfMissiles;
        missiles[numOfMissiles] = missile;
        numOfMissiles++;
        printf("Missile created, numOfMissiles: %d\n", numOfMissiles);
        return missile;
    }
    printf("Too many missiles fired already.\n");
    return NULL;
}

void DeleteMissile(Missile *missile)
{
    free(missile);
}

给我这个错误:double free或corruption(fasttop):0x000000000234f5d0 当函数DeleteMissile(Missile *missile)被调用时。我已经尝试用free(missile);替换free(missiles[missile->id]);free()在导弹的全局数组中找到(相同的)导弹并在那个导弹上调用LoadModelPlus(),这给了我同样的错误。函数missile->model使用malloc作为{{1}}。

谁能看到我做错了什么?

0 个答案:

没有答案