如何释放libjpeg创建的缓冲区

How to release buffer created by libjpeg?

本文关键字:libjpeg 创建 缓冲区 释放 何释放      更新时间:2023-10-16

我正在使用libjpeg从OpenCV Mat转换图像缓冲区并将其写入内存位置

代码如下:

bool mat2jpeg(cv::Mat frame, unsigned char **outbuffer
    , long unsigned int *outlen) {
    unsigned char *outdata = frame.data;
    struct jpeg_compress_struct cinfo = { 0 };
    struct jpeg_error_mgr jerr;
    JSAMPROW row_ptr[1];
    int row_stride;
    *outbuffer = NULL;
    *outlen = 0;
    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);
    jpeg_mem_dest(&cinfo, outbuffer, outlen);
    jpeg_set_quality(&cinfo, JPEG_QUALITY, TRUE);
    cinfo.image_width = frame.cols;
    cinfo.image_height = frame.rows;
    cinfo.input_components = 1;
    cinfo.in_color_space = JCS_GRAYSCALE;
    jpeg_set_defaults(&cinfo);
    jpeg_start_compress(&cinfo, TRUE);
    row_stride = frame.cols;
    while (cinfo.next_scanline < cinfo.image_height) {
        row_ptr[0] = &outdata[cinfo.next_scanline * row_stride];
        jpeg_write_scanlines(&cinfo, row_ptr, 1);
    }
    jpeg_finish_compress(&cinfo);
    jpeg_destroy_compress(&cinfo);

    return true;
}

问题是我不能在任何地方释放溢出缓冲区

我是这样使用这个函数的:

long unsigned int * __size__ = nullptr;
unsigned char * _buf = nullptr;
mat2jpeg(_img, &_buf, __size__);

free(_buf)和free(*_buf)都失败我这样做似乎是在试图释放堆头。

和mat2jpeg不接受指向外缓冲区指针的指针。任何想法?

我认为您的问题可能与您的__size__变量有关。它没有分配到任何地方。根据我对libjpeg源代码的阅读,这意味着缓冲区永远不会分配,程序调用一个致命错误函数。

我认为你应该这样称呼它:

long unsigned int __size__ = 0; // not a pointer
unsigned char * _buf = nullptr;
mat2jpeg(_img, &_buf, &__size__); // send address of __size__

那么你应该能够使用:

释放缓冲区
free(_buf);

我已经验证了是dll导致了这个问题。我尝试将libjpeg重新编译为静态库,现在一切都像魅力一样工作。

在我的情况下,没有办法释放内存图像指针,唯一的方法是为图像预留足够的内存,这样库就不会为我预留内存,我可以控制内存,内存将是我自己的应用程序的一部分,而不是库的dll或。lib:

//previous code...
struct jpeg_compress_struct cinfo;
//reserving the enough memory for my image (width * height)
unsigned char* _image = (unsigned char*)malloc(Width * Height);
//putting the reserved size into _imageSize
_imageSize = Width * Height;
//call the function like this:
jpeg_mem_dest(&cinfo, &_image, &_imageSize);
................
//releasing the reserved memory
free(_image);

注意:如果你放了_imageSize = 0,库会认为你没有预留内存,而自己的库会这样做。因此,您需要在_imageSize中放入_image中保留的字节数

这样你就可以完全控制预留的内存,你可以在你的软件中随时释放它。