title: System.AccessViolationException while compressing JPEG using jpeglib
body: <p>I'm rather new with jpeglib. I encounter the following problem. I have to save a rgb raster to a jpeg file. The code :</p>
  
  <pre><code>typedef struct JpegErrorManager {
      struct jpeg_error_mgr pub;
      jmp_buf setjmp_buffer; //callback
  } JpegErrorManager;
  
  void JpegErrorExit(j_common_ptr cinfo)
  {
      /* cinfo-&gt;err actually points to a jpegErrorManager struct */
      JpegErrorManager* myerr = (JpegErrorManager*)cinfo-&gt;err;
  
      /* Create the message */
      (*(cinfo-&gt;err-&gt;format_message)) (cinfo, jpegLastErrorMsg);
  
      /* Jump to the setjmp point */
      longjmp(myerr-&gt;setjmp_buffer, 1);
  }
  
  int Write24ByteRaster2JpegFile(const char *filefullpath, char* raster, int width, int heigth)
  {
      struct jpeg_compress_struct info;
      //struct jpeg_error_mgr jerr;
      unsigned char *oneRowImgData;
  
      FILE* file = fopen(filefullpath, "wb");  
      if (!file) {
          SetError("Error when creating jpeg file");
          return SIL_ERROR;
      }
  
      //Overriding jpeglib standard behavior on errors by setting specific error handler
      JpegErrorManager jerr;
      info.err = jpeg_std_error(&amp;jerr.pub);       
      jerr.pub.error_exit = JpegErrorExit;    
      if (setjmp(jerr.setjmp_buffer))         //Error "callback"
      {
          char errormsg[210];
          sprintf(errormsg, "JPEGLIB : %s", jpegLastErrorMsg);
          SetError(errormsg);
  
          jpeg_destroy_compress(&amp;info);
          fclose(file);
  
          return SIL_ERROR;
      }
  
      // Compression parameters
      //info.err = jpeg_std_error(&amp;jerr);  //&gt; Uncomment to enable jpeglib std error behavior
      jpeg_stdio_dest(&amp;info, file);
      info.image_width = width;
      info.image_height = heigth;
      info.input_components = 3;
      info.in_color_space = JCS_RGB;  
      jpeg_set_defaults(&amp;info);   
  
      jpeg_start_compress(&amp;info, TRUE);
  
      // Define an array, the representative picture of each row of data.
      int buffsz = width * info.input_components;
      oneRowImgData = (unsigned char*)malloc((size_t)buffsz);
      if (!oneRowImgData)
      {
          SetError("Memory allocation error while writing jpeg");
          return SIL_ERROR;
      }
      memset(oneRowImgData, 0, buffsz);
  
      JSAMPROW row_pointer[1];
      row_pointer[0] = oneRowImgData;
      for (int i = 0; i &lt;info.image_height; ++i)
      {
          oneRowImgData[i] = i % 256; // Dummy image data
          jpeg_write_scanlines(&amp;info, row_pointer, 1);
      }
  
      free(oneRowImgData);
      jpeg_finish_compress(&amp;info);
      jpeg_destroy_compress(&amp;info);
  
      fclose(file); 
      file = NULL;
  
      return SIL_SUCCESS;
  }
  </code></pre>
  
  <p>The error System.AccessViolationException occurs at the call to jpeg_stdio_dest. </p>
  
  <p>I checked  :
  Is it my specific error handler that causes the problem ? It <em>seems</em> not to be the source. First because i use the same error handler in all my other functions (reading jpeg) an it works perfect, and second because i tried to set back the standard jpeglib error behavior and had the same error.</p>
  
  <p>Right issue when writing file ? Il checked if i had the right to create / write file in the path specified in "filefullpath". I have. </p>
  
  <p>I have no idea what to check next. Thanks for your help.</p>