0

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?

Draex_
  • 3,011
  • 4
  • 32
  • 50
  • 1
    Release is intended to be fast, so it assumes your logic is correct. Debug, on the other hand, is intended to make finding bugs easier and does more checking. Looks like it found a bug. – user4581301 Mar 16 '19 at 20:27
  • 1
    When the program halts because of the error, take the IDE up on its offer to allow you to debug. Then look at the backtrace to see how the program got to the crash site. Keep an eye on what values were passed into the function that crashed. At least one of them is wrong. – user4581301 Mar 16 '19 at 20:29
  • Verify in debugger that all members of `cinfo` are initialized. In debug mode uninitialized memory may contain values different to zero. – Tobias Wollgam Mar 16 '19 at 23:12
  • @TobiasWollgam Most members seem to be initialized in both Debug and Release: https://i.snag.gy/EhGeTU.jpg – Draex_ Mar 18 '19 at 09:16
  • `client_data` containes an invalid address in debug mode. May be a problem. – Tobias Wollgam Mar 18 '19 at 09:38

1 Answers1

0

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

Draex_
  • 3,011
  • 4
  • 32
  • 50