How can I fix this error Access violation reading

2019-07-17 21:39发布

This question already has an answer here:

First-chance exception at 0x00814477 in Only time for one more.exe: 0xC0000005: Access violation reading location 0x00000008. Unhandled exception at 0x00814477 in Only time for one more.exe: 0xC0000005: Access violation reading location 0x00000008.

//I believe this has something to do with NULL and the OS but I am unsure how to fix this

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 
}

标签: c++ null
2条回答
我命由我不由天
2楼-- · 2019-07-17 22:07

This function call:

sprite = IMG_Load( filename.c_str() );

Is likely returning NULL. You should check the relevant documentation for why that might be and correct the related error.

查看更多
孤傲高冷的网名
3楼-- · 2019-07-17 22:07

Sprite seems to be null. Check the return of IMG_Load. Most likely your filename isn't correct.

查看更多
登录 后发表回答