How can I fix this error Access violation reading

2019-07-17 22:20发布

问题:

This question already has an answer here:

  • 0xC0000005: Access violation reading location 0x00000008 1 answer

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 
}

回答1:

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.



回答2:

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



标签: c++ null