gpt4 book ai didi

c - 当 jpeg_mem_dest 分配空闲缓冲区时发生错误访问

转载 作者:太空宇宙 更新时间:2023-11-04 03:29:33 25 4
gpt4 key购买 nike

我正在使用 jpeglib 压缩 jpeg。正如文档所说,我需要自己释放缓冲区“mem”。但是当 jpeg_finish_compress 或 jpeg_destroy_compress 之后的 free(mem) 时它崩溃了,说 access violate。

还有一个人和我有同样的问题。

我有什么错误吗?谢谢!

/* 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 的变量。

关于c - 当 jpeg_mem_dest 分配空闲缓冲区时发生错误访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38049693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com