1

I'm using jpeglib to compress a jpeg. As document says, I need to release the buffer 'mem' by myself. But it crashed when free(mem) after jpeg_finish_compress or jpeg_destroy_compress, saying access violate.

There was another person who had the same question with me.

Did I have any mistakes? thank you!

/* Step 1: allocate and initialize JPEG compression object */
jpeg_compress_struct cinfo;
jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;

cinfo.image_width = width;
cinfo.image_height = height;
unsigned char* mem = NULL;
unsigned long memSize = 0;
jpeg_mem_dest(&cinfo, &mem, &memSize);

jpeg_set_defaults(&cinfo);

jpeg_set_quality(&cinfo, quality, TRUE);

/* Step 4: Start compressor */
jpeg_start_compress(&cinfo, true);

/* pointer to JSAMPLE row[s] */
JSAMPROW row_pointer[1];

/* Step 5: while (scan lines remain to be written) */
/*           jpeg_write_scanlines(...); */
while (cinfo.next_scanline < cinfo.image_height) {
    row_pointer[0] = &newImgData[cinfo.next_scanline * width * 3];
    jpeg_write_scanlines(&cinfo, row_pointer, 1);
}

/* Step 6: Finish compression */
jpeg_finish_compress(&cinfo);
//free(newImgData);
/* Step 7: release JPEG compression object */
/* This is an important step since it will release a good deal of memory. */
jpeg_destroy_compress(&cinfo);

free(newImgData);
zhjq
  • 11
  • 1
  • There was another person who had the same question with me.http://stackoverflow.com/questions/32587235/how-to-release-buffer-created-by-libjpeg – zhjq Jun 27 '16 at 08:59

1 Answers1

1

The code doesn't make sense, the dynamically allocated buffer is in the variable called mem but you are attempting to free the variable called newImgData.

Étienne
  • 4,773
  • 2
  • 33
  • 58