如何修复此错误读取位置0x00000008时发生访问冲突

How can I fix this error Access violation reading location 0x00000008

本文关键字:访问冲突 0x00000008 位置 何修复 错误 读取      更新时间:2023-10-16

在0x00814477处的第一次机会异常。仅限一次。exe:0xC0000005:读取位置0x00000008的访问冲突。0x00814477处未处理的异常,仅限一次。exe:0xC0000005:读取位置0x00000008时发生访问冲突。

//我相信这与NULL和操作系统有关,但我不确定如何修复这个

GameObject::GameObject()
{
    sprite = NULL;
    pos.x = 0; pos.y = 0;
    vel.x = 0; vel.y = 0;
    framenum  = 0;
    numframes = 0;
}
GameObject::~GameObject()
{
    if ( sprite != NULL )
        SDL_FreeSurface( sprite );
}   
void GameObject::LoadImage(std::string filename)
{
    sprite = IMG_Load( filename.c_str() );
    //clip.x = 0;
    //clip.y = 0;
    clip.w = sprite->w;           //It breaks here 
    clip.h = sprite->h;          // and here 
}

此函数调用:

sprite = IMG_Load( filename.c_str() );

很可能返回NULL。您应该查看相关文档以了解原因,并更正相关错误。

Sprite似乎为null。检查IMG_Load的返回情况。很可能你的文件名不正确。