簡體   English   中英

在0x00000000時發生訪問沖突使用SDL_image庫加載PNG文件

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

我在Visual C ++ 2010 Express中將SDL2 2.0.3庫與SDL_image庫2.0.0一起使用。 我正在利用SDL_image庫從資源文件夾中加載各種PNG和JPEG文件。 雖然庫初始化沒有任何錯誤並加載了BMP和JPEG文件,但在給定PNG文件時它會中斷。

“ appname.exe中0x00000000處未處理的異常:0xC0000005:訪問沖突。”

在我的紋理管理器內部(一個存儲和管理程序紋理的對象),是一個從給定文件名字符串中加載紋理的函數。 這是代碼,包括我在實現SDL_image進行加載之前使用的注釋行。 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;
}

調用堆棧:

    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()  

這是我的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;
    }
}

供您參考,我使用的是最新的Windows 10 x64。 雙NVidia GTX 550ti的圖形驅動程序也是最新的。 所有DLL文件(包括pnglib dll)都位於debug文件夾中並進行加載。 如果我從程序中刪除了DLL文件,則圖像無法加載,並提供了上面針對NULL面編碼的消息。 沒有例外發生。

問題摘要:為什么會引發此異常,為什么僅對PNG文件引發此異常,並且當調用堆棧的詳細信息在看來正常工作的調用結束時如何跟蹤? 我是在做錯什么,還是可能錯過了配置步驟?

編輯 :感謝@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() 

這表明問題發生在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報告此時lib為NULL,這將解釋該錯誤! 問題是,為什么它為NULL? 似乎該代碼應該對此進行檢查,但盡管如此,全能的互聯網上似乎沒有其他人遇到此問題。

非常感謝大家的幫助,但是,像往常一樣,問題很明顯! 如@Gigi所述: SDL_Image IMG_Load在png上失敗,並帶有:“無法加載libpng16-16.dll:”

我建議您嘗試將所有其他內容都包括在內-可能存在您和我不知道的依賴性。 實際上我剛剛檢查過,而libpng需要zlib:libpng.org/pub/png/libpng.html

我不確定為什么我最初的搜索或建議沒有把該帖子發布出來。

我最初排除了我沒有使用(或在代碼中初始化)的其他文件格式的DLL,但是一旦包含zlib DLL bingo。 PNG完全按預期加載。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM