0x00000000处的访问冲突正在使用SDL_image库加载PNG文件
Access violation at 0x00000000 Loading PNG File With SDL_image Library
我在Visual C++2010学习版中使用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: %sn", IMG_GetError());
}
else
{
cout << "SDL_Image initialized for JPEG and PNG support" << endl;
}
}
供您参考,我使用的是最新的Windows 10 x64。双NVidia GTX 550ti的图形驱动程序也是最新的。所有DLL文件(包括pnglib DLL)都在调试文件夹中,并进行加载。如果我从程序中删除DLL文件,图像将无法加载,并提供上面为NULL曲面编码的消息。没有出现任何例外。
问题摘要:为什么会引发这个异常,为什么它只针对PNG文件引发,以及当调用堆栈的详细信息在我的调用中结束时,我如何跟踪它?我是做错了什么,还是错过了配置步骤?
EDIT:多亏了@南都,我重新编译了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,宾果。PNG的加载完全符合预期。
- std::原子加载和存储都需要吗
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 为什么加载SDF会导致Mobilizer创建闭环错误
- C++atioglxx.pdb未加载错误glBufferData OpenGL
- 如何使用tinyxml2从XML加载父实体和子实体
- 如何在C++中使用pybind11加载一个pickle python列表
- 系统.将数组移交给c#中动态加载的c++DLL时发生AccessViolationException
- 当我尝试加载内核模块时,如何修复C++中的这个 malloc() 错误?
- 树莓上的 Libtorch 无法加载 pt 文件,但在 ubuntu 上工作
- Python ctypes:不会按预期加载 dll
- 为什么会出现 gettnig 运行时错误:加载类型为"_Bit_type"(stl_bvector.h) 的空指针?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 保存/加载大量短数组到二进制文件
- 使用 SDL2 和SDL_image C++加载图像
- 无法使用加载库卸载注入的 dll (C++)
- 是否有原子加载非原子值的函数?
- 将 bmp 文件加载到 TSpeedButton 中
- 如何使用Qt 3D库加载和显示搅拌机.obj源文件场景
- 窗口加载菜单错误: "The specified resource name cannot be found in the image file."