简体   繁体   English

libpng 1.616在VS2012 C ++中的png_read_png上崩溃

[英]libpng 1.616 crashing on png_read_png in VS2012 C++

I am on win7 64bit home edition. 我正在使用win7 64位家庭版。 All of my projects are set to build in a 32bit environment using C++. 我所有的项目都设置为使用C ++在32位环境中构建。 I have successfully built libpng in debug mode using MDd runtime. 我已经使用MDd运行时在调试模式下成功构建了libpng。 I have two active projects. 我有两个活跃的项目。 My first project is in VS2010 and my second is in VS2012. 我的第一个项目在VS2010中,而我的第二个项目在VS2012中。 Within my computer I created an environment variable set to the path for this library to make linking easier in my projects. 在我的计算机中,我创建了一个环境变量,该变量设置为此库的路径,以使在项目中的链接更加容易。 Both my projects are using MDd as well as multibyte which is the same that libpng uses. 我的两个项目都使用MDd以及多字节,这与libpng使用的相同。 All of my paths and dependencies are correct. 我所有的路径和依赖关系都是正确的。 I also copied over the libpng16.dll into both projects within the same directory as their built executable. 我还通过libpng16.dll将两个项目复制到与其生成的可执行文件相同的目录中。 I can successfully compile and build both projects. 我可以成功编译并构建两个项目。 My VS2010 project runs and renders the loaded PNG graphic, however my project in VS2012 doesn't. 我的VS2010项目运行并呈现已加载的PNG图形,但是我在VS2012中的项目却没有。 It crashes when I call png_read_png throwing and unhandled exception that breaks on this line: 当我调用png_read_png引发并在此行中断的未处理异常时,它会崩溃:

check = fread( data, 1, length, png_voidcast( png_FILE_p, png_ptr->io_ptr ) );

within pngrio.c. 在pngrio.c中。 giving this error: 给出此错误:

First-chance exception at 0x77308E19 (ntdll.dll) in Game_debug.exe: 0xC0000005: 
Access violation writing location 0x00000014.

The problem is not within my source code since I know that it works in my VS2010 project and I am using the same implementation to load the png in my VS2012 project. 这个问题不在我的源代码中,因为我知道它可以在我的VS2010项目中使用,并且我正在使用相同的实现在VS2012项目中加载png。 I am not sure off hand if it makes a difference that the library I am linking to was built in VS2010 or if there are some command line / compiler settings within the VS2010 built library that my VS2012 project doesn't like. 我不确定要链接的库是在VS2010中构建的,还是在VS2010构建的库中有一些命令行/编译器设置让我的VS2012项目不喜欢的,是否会有所不同。 I've read through the png documentation and searched online for hours and couldn't seem to find anything relevant. 我已经阅读了png文档,并在网上搜索了几个小时,但似乎找不到任何相关内容。 Any kind of help, tips, pointers or suggestions would do me a great deal. 任何形式的帮助,技巧,指示或建议都会对我有很大帮助。

Read the file projects/vstudio/readme.txt in the libpng source code, in particular the paragraph starting at line 41. 阅读libpng源代码中的文件projects / vstudio / readme.txt,尤其是从第41行开始的段落。

You are passing a (FILE*) to libpng, probably using png_init_io. 您正在将(FILE *)传递给libpng,可能是使用png_init_io。 Visual Studio crashes inside fread when it tries to access the underlying FILE structure. 当Visual Studio尝试访问基础FILE结构时,它在fread内部崩溃。

This is because you created the FILE* using fopen from one Visual Studio runtime but libpng is linked against a different Visual Studio runtime (msvcrt or something like that); 这是因为您从一个Visual Studio运行时使用fopen创建了FILE *,但是libpng被链接到另一个Visual Studio运行时(msvcrt或类似的东西)。 the two runtimes are incompatible. 这两个运行时不兼容。

There are quite a number of ways to cause this to happen but it will almost certainly happen if you use a zlib DLL. 有很多方法可以使这种情况发生,但是如果使用zlib DLL,几乎可以肯定会发生这种情况。 Are you using projects/vstudio to build libpng or did you try to do it yourself? 您是使用projects / vstudio来构建libpng还是尝试自己做? If you didn't use projects/vstudio you are on your own ;-) 如果您不使用项目/ vstudio,那么您就一个人了;-)

1) Don't build both libpng and zlib into separate DLLs; 1)不要将libpng和zlib都构建到单独的DLL中; link a libpng DLL against a static zlib (this is what projects/vstudio does) or use a static libpng (not a DLL) in your project (projects/vstudio also builds one of these.) 将libpng DLL链接到静态zlib(这是projects / vstudio所做的),或在项目中使用静态libpng(不是DLL)(projects / vstudio也会构建其中之一)。

2) Read up on the various Visual Studio runtimes. 2)阅读各种Visual Studio运行时。 Everything that you run must use exactly the same runtime; 您运行的所有内容都必须使用完全相同的运行时。 check every project to see what runtime it has. 检查每个项目以查看其具有的运行时。 Preferably use the default (/MD) 最好使用默认值(/ MD)

3) If you think you have encountered a bug in libpng link your application statically (ie don't use any DLLs built by Visual Studio) and try again. 3)如果您认为遇到libpng中的错误,请静态链接您的应用程序(即,不要使用Visual Studio构建的任何DLL),然后重试。 Windows DLLs are fine; Windows DLL很好; it's the Visual Studio ones that use the Visual Studio runtime that break everything. 是使用Visual Studio运行时的Visual Studio破坏了一切。

4) If the problem happens when you link statically make sure your DEBUG settings are the same everywhere. 4)如果在静态链接时发生问题,请确保您的DEBUG设置在所有地方都相同。 You can't mix'n'max DEBUG because the debug version of the MSVC runtimes (any of them, even pre-Visual Studio) are not compatible with the release versions. 您不能混合使用'n'max DEBUG,因为MSVC运行时的调试版本(其中任何一个,甚至是Visual Studio之前的版本)都与发行版本不兼容。

In fact it is better not to use png_init_io ; 实际上, 最好不要使用png_init_io libpng supports this. libpng支持这一点。 Provide your own read and write callbacks. 提供您自己的读写回调。 You can use stdio in those (fread) safely because they are implemented in the same DLL (the one you write) as calls fopen. 可以安全地在这些(读取)中使用stdio,因为它们在与调用fopen相同的DLL(您编写的DLL)中实现。 So long as you don't pass a (FILE*) across a DLL boundary you should be safe, maybe; 只要您不跨DLL边界传递(FILE *),也许就应该安全了; no one has ever explained or understood the two DLL (zlib, libpng) behavior. 没有人能解释或理解这两种DLL(zlib,libpng)的行为。

John Bowler 约翰·鲍勒

After considerable hours of trial and errors, and with the little bit of help from others I was finally able to successfully build my 2012 project using the same version of libpng. 经过数小时的试验和错误,并且在其他人的一点帮助下,我终于能够使用相同版本的libpng成功构建我的2012年项目。 What I had to do was in my directories where the libpng is located I had to create 3 Folders 我要做的是在我的libpng所在的目录中,我必须创建3个文件夹

  1. VS2010\\lpng1616\\ , VS2010\\zlib-1.2.8 VS2010 \\ lpng1616 \\,VS2010 \\ zlib-1.2.8
  2. VS2012\\lpng1616\\ , VS2012\\zlib-1.2.8 VS2012 \\ lpng1616 \\,VS2012 \\ zlib-1.2.8
  3. VS2013\\lpng1616\\ , VS2013\\zlib-1.2.8 VS2013 \\ lpng1616 \\,VS2013 \\ zlib-1.2.8

And within each of these 3 folders I had to open libpng' solution within the respected versions of Visual Studio. 在这三个文件夹中的每个文件夹中,我都必须在受尊重的Visual Studio版本中打开libpng解决方案。 Build out both release and debugs. 构建发布和调试。

Then on my computer for the environment variables, I had to create not one but 3. 然后在我的计算机上输入环境变量,我必须创建一个而不是3。

  1. PNG_SDK_2010 set to VS2010\\lpng1616\\ PNG_SDK_2010设置为VS2010 \\ lpng1616 \\
  2. PNG_SDK_2012 " VS2012\\lpng1616\\ PNG_SDK_2012“ VS2012 \\ lpng1616 \\
  3. PNG_SDK_2013 " VS2013\\lpng1616\\ PNG_SDK_2013“ VS2013 \\ lpng1616 \\

Then go back into my VS2010 and VS2012 projects set the paths using the environment variables accordingly replace the old stale libraries with the newly built ones, did a clean of the solution and both projects work properly! 然后回到我的VS2010和VS2012项目中,使用环境变量设置路径,从而用新构建的库替换旧的过时的库,清理解决方案,并且两个项目都能正常工作!

So in conclusion if you are using outside libraries that you can download from the net that comes with a solution that you need to open and build out yourself to link against and you need this same library for projects across different versions of Visual Studio then you will have to open the solution that came with that library within the same version of Visual Studio that you are planning on using with your current project. 因此,总而言之,如果您使用的是外部库,您可以从网络中下载该库,而解决方案则需要您打开并自行构建以进行链接,并且对于跨Visual Studio不同版本的项目都需要使用相同的库,那么您将必须在您计划与当前项目一起使用的相同版本的Visual Studio中打开该库随附的解决方案。 I hope that my own situation comes to serve as an answer for others! 我希望我自己的情况可以为他人提供答案!

Just faced the same issue with libpng and curl Both lib and curl use "msvcrt.dll" So I just added nasty workaround 刚遇到libpng和curl的相同问题lib和curl都使用“ msvcrt.dll”,所以我只添加了讨厌的解决方法

HMODULE h = LoadLibraryA("msvcrt.dll");
typedef FILE * (*FP)(char *f, char *m);
FP myf = (FP)GetProcAddress(h, "fopen");
if (myf)
{
    fp = myf((char *)lpszFileName, "wb");
}

and it worked for me. 它为我工作。 Smells bad, but no matter 闻起来不好,但是没关系

Slso, the advantage is that it works well for both debug and release version 另外,优点是它既适用于调试版本,也适用于发行版本

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

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