c++ - 如何修复此错误访问冲突读取位置 0x00000008

标签 c++ null

<分区>

Only time for one more.exe 中 0x00814477 处的第一次机会异常:0xC0000005:访问冲突读取位置 0x00000008。 Only time for one more.exe 中 0x00814477 处的未处理异常: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。您应该检查相关文档以了解原因并更正相关错误。

关于c++ - 如何修复此错误访问冲突读取位置 0x00000008,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17665147/

相关文章:

c++ - 未执行左值到右值的转换

c++ - 从 native 句柄创建 std::thread?

c++ - 如何从点列表到对象

java - 为什么我的 Spring @Autowired 字段为空?

mysql - HeidiSQL 导入csv空字段

c++ - 运算符重载

c++ - 错误: expected ‘)’ before ‘*’ token despite not include-loop

MySQL 合并非空值与其他 case when 和 coalesce 语句冲突 - 不确定如何修复

function - 不能将 nil 传递给两个可选参数

c# xml序列化不写null