【问题标题】:CImg throws an exception in Debug mode, works fine in ReleaseCImg 在 Debug 模式下抛出异常,在 Release 中工作正常
【发布时间】:2019-03-16 20:21:55
【问题描述】:

我的程序是用 C++ 编写的。我使用 Visual Studio 2017。

我的代码编译并在发布模式下正确运行,但在调试模式下抛出异常

在 0x00007FF9A4EAD428 (ucrtbase.dll) 中未处理的异常 MyAssemblyName.exe:将无效参数传递给函数 认为无效参数是致命的。

这个异常是在img.assign()函数中抛出的

CImg<unsigned char> img;

try {
    img.assign(picPath.c_str());
}
catch (CImgException) {
    // ...
}

在 CImg 中,这是正在执行的代码:

std::FILE *const nfile = file?file:cimg::fopen(filename,"rb");
      struct jpeg_decompress_struct cinfo;
      struct _cimg_error_mgr jerr;
      cinfo.err = jpeg_std_error(&jerr.original);
      jerr.original.error_exit = _cimg_jpeg_error_exit;
      if (setjmp(jerr.setjmp_buffer)) { // JPEG error
        if (!file) cimg::fclose(nfile);
        throw CImgIOException(_cimg_instance
                             "load_jpeg(): Error message returned by libjpeg: %s.",
                             cimg_instance,jerr.message);
      }

      jpeg_create_decompress(&cinfo);
      jpeg_stdio_src(&cinfo,nfile);
      jpeg_read_header(&cinfo,TRUE);
      jpeg_start_decompress(&cinfo);

执行jpeg_read_header()时抛出异常。

为什么会这样?为什么只在 Debug 模式下,而不是 Release?

【问题讨论】:

  • Release 旨在快速发布,因此它假定您的逻辑是正确的。另一方面,调试旨在使查找错误更容易并进行更多检查。看起来它发现了一个错误。
  • 当程序因错误而停止时,请使用 IDE 以允许您进行调试。然后查看回溯,看看程序是如何到达崩溃现场的。密切注意将哪些值传递到崩溃的函数中。其中至少有一个是错误的。
  • 在调试器中验证cinfo 的所有成员都已初始化。在调试模式下,未初始化的内存可能包含不为零的值。
  • @TobiasWollgam 大多数成员似乎在调试和发布中都已初始化:i.snag.gy/EhGeTU.jpg
  • client_data 在调试模式下包含无效地址。可能是个问题。

标签: c++ libjpeg cimg


【解决方案1】:

我已经按照this answer更新了jpeg库,问题就消失了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    相关资源
    最近更新 更多