我正在使用jpeglib压缩jpeg。正如文档所说,我需要自己释放缓冲区“mem”。但在jpeg完成压缩或jpeg破坏压缩后,当free(mem)出现访问冲突时,它崩溃了。
还有一个人和我有同样的问题。
我有什么错误吗?谢谢您!
/* 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);
最佳答案
代码没有意义,动态分配的缓冲区在名为mem
的变量中,但您正在尝试释放名为newImgData
的变量。