gpt4 book ai didi

c++ - 奇怪的未处理异常;访问违规阅读位置

转载 作者:太空宇宙 更新时间:2023-11-04 11:55:14 25 4
gpt4 key购买 nike

当我尝试调用 SDL_DisplayFormatAlpha 时,出现未处理的异常:“herorpg.exe 中 0x68125981 (SDL.dll) 处的未处理异常:0xC0000005:访问冲突读取位置 0x0000013C。”

代码:

SDL_Surface* load_image ( char* filename ) {
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load ( filename );

if ( loadedImage != NULL ) {
optimizedImage = SDL_DisplayFormatAlpha ( loadedImage );
SDL_FreeSurface ( loadedImage );
}

return optimizedImage;
}

bool init ( SDL_Surface*& screen ) {

//Initialize SDL
if ( SDL_Init ( SDL_INIT_EVERYTHING ) == -1 ) return false;
screen = SDL_SetVideoMode ( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_FULLSCREEN );
if ( screen == NULL ) return false;

//Set the caption
SDL_WM_SetCaption ( "Hero RPG", NULL );

return true;
}

主要内容:

int main ( int argc, char** argv ) {
init ( screen );

load_image ( "graphics\\background\\overworld" );

return 0;
}

其他:

如果我注释掉

    //if ( loadedImage != NULL ) {
// optimizedImage = SDL_DisplayFormatAlpha ( loadedImage );
// SDL_FreeSurface ( loadedImage );
//}

和改变

    loadedImage = IMG_Load ( filename );

    optimizedImage = IMG_Load ( filename );

图像加载正常。所以我知道文件名没有问题。

问题:

这正是我在上次设计的程序中使用的函数,上次它运行良好。我认为这个问题的答案是开箱即用的。为什么我会收到这个未处理的异常?

最佳答案

documentation它有这一行:

Of course, the video surface must be initialised using SDL_SetVideoMode before this function is called, or it will segfault.

看到我在您发布的代码中没有看到对 SDL_SetVideoMode 的调用,我可以假设这可能是原因。

关于c++ - 奇怪的未处理异常;访问违规阅读位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16441112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com