调用 SDL_RenderCopy 时出现异常

Exception when calling SDL_RenderCopy

我对 C++ 比较陌生,我正在使用 SDL 2.0 学习它。我在尝试使用 Sprite class:

绘制精灵时遇到以下错误

在 SDDDDL2.exe 中的 0x000000006C793659 (SDL2.dll) 抛出异常:0xC0000005:访问冲突读取位置 0xFFFFFFFFFFFFFFFF。

以下代码是我的 Sprite class 中相关代码的精简版:

public:
SDL_Texture *image = NULL;
SDL_Rect rect;

void SetTexture(SDL_Texture *texture) 
{
    image = texture;
    rect.x = 100; rect.y = 100; rect.w = 64; rect.h = 64;
}

void DrawSprite(SDL_Renderer *renderer) 
{
    SDL_RenderCopy(renderer,image,NULL,&rect); //Calling this causes the
    //error
}

以及我主游戏中的关键代码class"Game.cpp"

Sprite *testSprite = NULL;
SDL_Texture *testTex = NULL;

void LoadContent() 
{
    SDL_Surface *bmpSurface = SDL_LoadBMP("sprite.bmp");
    testTex = SDL_CreateTextureFromSurface(renderer, bmpSurface);
    testSprite = &Sprite(Vector2(100,100),Vector2(50,50)); // Just the 
    //constuctor, this is not affecting the issue
    testSprite->SetTexture(testTex);
    SDL_FreeSurface(bmpSurface);
}

void Draw () 
{
    testSprite->DrawSprite(renderer); // Get the error when calling this
}

我通过测试知道确实是纹理被传递到 SDL_RenderCopy 函数 (image) 导致了这个问题,因为如果我使用 "testTex" 图像调用 Game.cpp 文件中的函数。

我也知道 SDL_RenderCopy 函数中使用的纹理不是 NULL,因为我在调用 SDL_RenderCopy 之前使用了空检查,它调用了无论如何。

我认为问题出在这一行:testSprite = &Sprite(Vector2(100,100), Vector2(50,50));

testSprite分配的地址值在LoadContent()returns后立即失效。

替换为例如testSprite = new Sprite(Vector2(100,100),Vector2(50,50)); 并重新 运行.