Search code examples
c++libjpegcimg

CImg throws an exception in Debug mode, works fine in Release


My program is written in C++. I use Visual Studio 2017.

My code compiles and runs correctly in Release mode, but throws exception in Debug mode:

Unhandled exception at 0x00007FF9A4EAD428 (ucrtbase.dll) in MyAssemblyName.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.

This exception is thrown in the img.assign() function

CImg<unsigned char> img;

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

In CImg, this is the code being executed:

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

The exception is thrown when executing jpeg_read_header().

Why is this happening? Why only in Debug mode, not Release?


Solution

  • I've updated the jpeg library according to this answer and the problem disappeared.