1

这是头文件中的结构:

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);
}

给我这个错误:双重释放或损坏(fasttop):DeleteMissile(Missile *missile)调用函数时为0x000000000234f5d0。我尝试用 替换free(missile);free(missiles[missile->id]);它在全球导弹阵列中找到(相同的)导弹并调用free()该导弹,这给了我同样的错误。该函数LoadModelPlus()将 malloc 用于missile->model.

谁能看到我做错了什么?

4

0 回答 0