内存管理代码崩溃

时间:2017-04-21 02:12:52

标签: c++ pointers memory malloc

我正在使用Scripthook制作GTA 5 mod。我没有任何问题地运行他们的示例代码,但是当我尝试自己创建时,它会使游戏崩溃。

#include "script.h"
#include "keyboard.h"

#include <string>
#include <ctime>

#pragma warning(disable : 4244 4305) // double <-> float conversions

#define ID 0
#define PED 1

LPINT currentPlayer() {
    LPINT ret = (LPINT)malloc(sizeof(int) * 2);
    ret[0] = PLAYER::PLAYER_ID();
    ret[1] = PLAYER::PLAYER_PED_ID();
    return ret;
}

void getPlayerCoords(LPVOID coords) {
    *(Vector3 *)coords = ENTITY::GET_ENTITY_COORDS(currentPlayer()[PED], true);
}

void spawnKuruma2() {
    Vector3 *currentCoordinates = (Vector3 *)malloc(sizeof(Vector3));
    getPlayerCoords(currentCoordinates);
    Hash kHash = GAMEPLAY::GET_HASH_KEY("KURUMA2");
    STREAMING::REQUEST_MODEL(kHash);
    while (!STREAMING::HAS_MODEL_LOADED(kHash));
    Vehicle kuruma2 = VEHICLE::CREATE_VEHICLE(kHash, (*currentCoordinates).x + 5.0, (*currentCoordinates).y + 5.0, (*currentCoordinates).z, 0.0, true, true);
    VEHICLE::SET_VEHICLE_ON_GROUND_PROPERLY(kuruma2);
    STREAMING::SET_MODEL_AS_NO_LONGER_NEEDED(kHash);
    ENTITY::SET_VEHICLE_AS_NO_LONGER_NEEDED(&kuruma2);
}

void main()
{   
    while (1)
    {
        if (IsKeyJustUp(VK_F8))
        {
            spawnKuruma2();
        }       
        WAIT(0);
    }
}

void ScriptMain()
{
    srand(GetTickCount());
    main();
}

我很肯定它与malloc调用有关,但它看起来很好。任何人都可以指出我可能做错了吗?

0 个答案:

没有答案