【问题标题】:error access violate when free buffer malloced by jpeg_mem_dest当 jpeg_mem_dest 分配空闲缓冲区时,错误访问冲突
【发布时间】:2016-06-27 08:57:42
【问题描述】:

我正在使用 jpeglib 来压缩 jpeg。正如文件所说,我需要自己释放缓冲区“mem”。但是在 jpeg_finish_compress 或 jpeg_destroy_compress 之后 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);

【问题讨论】:

标签: c libjpeg


【解决方案1】:

代码没有意义,动态分配的缓冲区位于名为 mem 的变量中,但您正试图释放名为 newImgData 的变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2017-05-12
    • 2021-12-21
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多