简体   繁体   English

在0x00000000时发生访问冲突使用SDL_image库加载PNG文件

[英]Access violation at 0x00000000 Loading PNG File With SDL_image Library

I am using the SDL2 2.0.3 library with the SDL_image library 2.0.0 in Visual C++ 2010 Express. 我在Visual C ++ 2010 Express中将SDL2 2.0.3库与SDL_image库2.0.0一起使用。 I am utilizing the SDL_image library to load a variety of PNG and JPEG files from a resources folder. 我正在利用SDL_image库从资源文件夹中加载各种PNG和JPEG文件。 While the library initializes without any errors and loads BMP and JPEG files, it breaks when given a PNG file. 虽然库初始化没有任何错误并加载了BMP和JPEG文件,但在给定PNG文件时它会中断。

"Unhandled exception at 0x00000000 in appname.exe: 0xC0000005: Access violation." “ appname.exe中0x00000000处未处理的异常:0xC0000005:访问冲突。”

Inside of my texture manager, (an object that stores and manages textures for the program), is a function to load a texture from a given file name string. 在我的纹理管理器内部(一个存储和管理程序纹理的对象),是一个从给定文件名字符串中加载纹理的函数。 Here is the code, including the commented line I used before implementing SDL_image to do the loading. 这是代码,包括我在实现SDL_image进行加载之前使用的注释行。 It is within the line bitmapSurface = IMG_Load(... that the exception above is thrown. bitmapSurface = IMG_Load(...行内,引发了以上异常。

/**
 * Attempts to load a given image file and reports to the console any failures
 * 
 * @param fileName The exact file name of the resource to be loaded
 * @return The SDL_Texture loaded from the given fileName
 */
SDL_Texture* TextureManager::loadTexture(string fileName)
{
    //Create our surface in RAM
    SDL_Surface *bitmapSurface = NULL;
    //bitmapSurface = SDL_LoadBMP(fileName.c_str()); //OLD METHOD; standard SDL, BMP only
    bitmapSurface = IMG_Load(fileName.c_str());      //NEW METHOD; SDL_image lib, many formats

    //Verify it exists
    if (bitmapSurface == NULL)
        cout << "Image resource not loaded: " << fileName << " Message: " << IMG_GetError() << endl;

    //Create a texture in VRAM from the surface
    SDL_Texture *bitmapTexture = NULL;
    bitmapTexture = SDL_CreateTextureFromSurface(this->renderer, bitmapSurface);

    //Verify it exists
    if (bitmapTexture == NULL)
        cout << "Failed to create texture: " << fileName << endl;

    return bitmapTexture;
}

Call Stack: 调用堆栈:

    00000000()  
    SDL2_image.dll!6a887a01()   
    [Frames below may be incorrect and/or missing, no symbols loaded for SDL2_image.dll]    
    SDL2.dll!6c77da4b()     
    SDL2_image.dll!6a88792e()   
    SDL2_image.dll!6a881693()   
    SDL2_image.dll!6a8817e9()   
> appname.exe!TextureManager::loadTexture(std::basic_string<char,std::char_traits<char>,std::allocator<char> > fileName)  Line 143 + 0xe bytes  C++
    00daf5e0()  

Here is the constructor for my TextureManager: 这是我的TextureManager的构造函数:

/**
 * Creates a new TextureManager with the current SDL_Renderer
 * 
 * @param renderer The current renderer instance of the current graphic window
 */ 
TextureManager::TextureManager(SDL_Renderer* renderer)
{
    //Assign our renderer link
    this->renderer = renderer;

    //Create the vector to hold textures
    this->textures = new vector<Texture*>();

    //SDL_image initialization
    int flags   = IMG_INIT_JPG|IMG_INIT_PNG;
    int initted = IMG_Init(flags); //Use IMG_Quit(); at closing

    if(initted&flags != flags)
    {
        //Handle error
        printf("IMG_Init: Failed to init required jpg and png support!\n");
        printf("IMG_Init: %s\n", IMG_GetError());
    }
    else
    {
        cout << "SDL_Image initialized for JPEG and PNG support" << endl;
    }
}

For your information, I am using Windows 10 x64, which is up to date. 供您参考,我使用的是最新的Windows 10 x64。 Graphics drivers for dual NVidia GTX 550ti are up to date as well. 双NVidia GTX 550ti的图形驱动程序也是最新的。 All DLL files (including the pnglib dll) are in the debug folder and do load. 所有DLL文件(包括pnglib dll)都位于debug文件夹中并进行加载。 If I remove the DLL file from the program, the image fails to load and provides the message as coded above for NULL surfaces. 如果我从程序中删除了DLL文件,则图像无法加载,并提供了上面针对NULL面编码的消息。 No exceptions occur. 没有例外发生。

Question summary: Why is this exception thrown, why is it only thrown for PNG files, and how can I trace it when the call stack's details end at my call that appears to work properly? 问题摘要:为什么会引发此异常,为什么仅对PNG文件引发此异常,并且当调用堆栈的详细信息在看来正常工作的调用结束时如何跟踪? Am I doing something wrong, or is there a configuration step I may have missed? 我是在做错什么,还是可能错过了配置步骤?

EDIT : Thanks to @Nandu I have recompiled the DLL SDL_image, and got a better call stack output here: 编辑 :感谢@Nandu,我重新编译了DLL SDL_image,并在这里获得了更好的调用堆栈输出:

    00000000()  
> SDL2_image.dll!IMG_LoadPNG_RW(SDL_RWops * src)  Line 375 + 0x11 bytes C
    SDL2_image.dll!IMG_LoadTyped_RW(SDL_RWops * src, int freesrc, const char * type)  Line 193 + 0x12 bytes C
    SDL2_image.dll!IMG_Load(const char * file)  Line 134 + 0xf bytes    C
    appname.exe!TextureManager::loadTexture(std::basic_string<char,std::char_traits<char>,std::allocator<char> > fileName)  Line 143 + 0xe bytes    C++
    appname.exe!TextureManager::loadFromDirectory(std::basic_string<char,std::char_traits<char>,std::allocator<char> > relPath)  Line 117 + 0x73 bytes  C++
    appname.exe!SDL_main(int argc, char * * argv)  Line 31  C++
    appname.exe!main(int argc, char * * argv)  Line 140 + 0xd bytes C
    appname.exe!__tmainCRTStartup()  Line 555 + 0x19 bytes  C
    appname.exe!mainCRTStartup()  Line 371  C
    kernel32.dll!77963744()     
    [Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]  
    ntdll.dll!77c3a064()    
    ntdll.dll!77c3a02f() 

This suggests the problem is occurring on line 374 of IMG_png.c: 这表明问题发生在IMG_png.c的第374行:

 /* Create the PNG loading context structure */
    png_ptr = lib.png_create_read_struct(PNG_LIBPNG_VER_STRING,
                      NULL,NULL,NULL);

VS reports that lib is NULL at this time, which would explain the error! VS报告此时lib为NULL,这将解释该错误! The question becomes, why is it NULL? 问题是,为什么它为NULL? It seems this code should be doing a check for that, but nonetheless, nobody else seems to having this problem on the almighty internet. 似乎该代码应该对此进行检查,但尽管如此,全能的互联网上似乎没有其他人遇到此问题。

Thank you all very much for you help, but the problem, as usual, was rather obvious! 非常感谢大家的帮助,但是,像往常一样,问题很明显! As noted here by @Gigi: SDL_Image IMG_Load fails on png with: "Failed loading libpng16-16.dll:" 如@Gigi所述: SDL_Image IMG_Load在png上失败,并带有:“无法加载libpng16-16.dll:”

I suggest you try including all the rest - there may be a dependency that you and I are not aware of. 我建议您尝试将所有其他内容都包括在内-可能存在您和我不知道的依赖性。 In fact I just checked, and libpng requires zlib: libpng.org/pub/png/libpng.html 实际上我刚刚检查过,而libpng需要zlib:libpng.org/pub/png/libpng.html

I'm not sure why my original search or suggestions didn't bring that post up. 我不确定为什么我最初的搜索或建议没有把该帖子发布出来。

I originally excluded the DLLs for other file formats I wasn't using (or initializing in my code), but once I included the zlib DLL, bingo. 我最初排除了我没有使用(或在代码中初始化)的其他文件格式的DLL,但是一旦包含zlib DLL bingo。 PNGs load exactly as expected. PNG完全按预期加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM