加载带有SDL_图像库的PNG文件时发生0x00000000访问冲突 我使用SDL2 2.0.3库,在Visual C++ 2010 Express中使用SDLIMAGE库2.0.0。我正在利用SDL_图像库从资源文件夹加载各种PNG和JPEG文件。虽然库初始化时没有任何错误,并加载BMP和JPEG文件,但在给定PNG文件时,它会中断
“appname.exe中0x00000000处的未处理异常:0xC0000005:访问冲突。” 在我的纹理管理器(一个为程序存储和管理纹理的对象)中,有一个函数可以从给定的文件名字符串加载纹理。下面是代码,包括我在实现SDL_映像进行加载之前使用的注释行。正是在加载带有SDL_图像库的PNG文件时发生0x00000000访问冲突 我使用SDL2 2.0.3库,在Visual C++ 2010 Express中使用SDLIMAGE库2.0.0。我正在利用SDL_图像库从资源文件夹加载各种PNG和JPEG文件。虽然库初始化时没有任何错误,并加载BMP和JPEG文件,但在给定PNG文件时,它会中断,c++,visual-studio-2010,sdl-2,sdl-image,C++,Visual Studio 2010,Sdl 2,Sdl Image,“appname.exe中0x00000000处的未处理异常:0xC0000005:访问冲突。” 在我的纹理管理器(一个为程序存储和管理纹理的对象)中,有一个函数可以从给定的文件名字符串加载纹理。下面是代码,包括我在实现SDL_映像进行加载之前使用的注释行。正是在bitmapSurface=IMG\u Load(…行)内引发了上述异常 /** * Attempts to load a given image file and reports to the console any failure
bitmapSurface=IMG\u 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;
}
VS报告说,
lib
此时为空,这可以解释错误!问题是,为什么为空?似乎这段代码应该对此进行检查,但尽管如此,在全能的互联网上似乎没有其他人有此问题。非常感谢大家的帮助,但问题与往常一样,非常严重显而易见!
正如@Gigi在此指出的:
我建议您尝试包含所有其他内容-可能存在您和我都不知道的依赖关系。事实上,我刚刚检查过,libpng需要zlib:libpng.org/pub/png/libpng.html
我不知道为什么我最初的搜索或建议没有带来这篇文章
我最初排除了我没有使用(或在代码中初始化)的其他文件格式的DLL,但一旦我包含了zlib DLL,bingo.PNGs加载将完全按照预期进行。您有代码要检查NULL返回值,但您的代码继续运行,就好像没有任何错误。
//如果(bitmapSurface==NULL)验证它是否存在
您是否拥有DLL文件?如果是,请在关闭优化的情况下重建它(同时关闭应用程序优化)。通过dll中内置的符号,您可以收集调用堆栈和这次崩溃的起因。@PaulMcKenzie是的,我的代码进一步抵抗空纹理,我只想得到警告。@Nandu我以前从未这样做过,但我会尝试一下。如果您有任何建议或教程,我们将不胜感激。我想说您的libpng library没有正确加载,在这种情况下,我会尝试从源代码构建整个东西并调试它。
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()
/**
* 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;
}
}
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()
/* Create the PNG loading context structure */
png_ptr = lib.png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL,NULL,NULL);