C++ 如何释放libjpeg创建的缓冲区?

C++ 如何释放libjpeg创建的缓冲区?,c++,opencv,memory-leaks,libjpeg,libjpeg-turbo,C++,Opencv,Memory Leaks,Libjpeg,Libjpeg Turbo,我正在使用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_m

我正在使用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;

}
free(_buf)和free(*_buf)都失败 看来我是想通过这样做来释放堆头


mat2jpeg不接受指向Exputffer的指针。有什么想法吗?

我想你的问题可能是你的
\uuuuuuuuuuuu
变量。它没有分配到任何地方。根据我对的理解,这意味着从未分配缓冲区,程序调用致命错误函数

我想你应该这样称呼它:

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);
注意:如果您将
\u imageSize=0
,则库将假定您没有保留内存,而自己的库将执行此操作。。因此,您需要输入
\u imageSize
\u image
中保留的字节数


这样,您就可以完全控制保留内存,并且可以在软件中随时释放它。

我已根据您的评论进行了更改。错误仍然存在。我认为问题可能是由于释放了在第三方dll中分配的内存。所以我试图释放jpeg.dll分配的内存。但是dll有不同的堆管理器。我将进一步从源代码构建来验证这一点。这个答案是正确的,还请注意,只有在调用了函数jpeg\u finish\u compress之后,您才能调用free(_buf),否则您将得到一个segfault。这样我无法解决问题,既不能使用.dll也不能使用.lib。。每当我尝试调用free(_buf)时,总是给我一个错误。@KronuZ此答案正确地修复了此问题中提供的代码的一个问题(发送未分配整数的地址)。如果它不能修复你的代码,为什么不在你的特定代码和特定问题中发布你自己的问题呢?这里的问题实际上是libjpeg的问题——它似乎希望你在应该公开一个函数时释放内存,以便你调用它释放内存,从而解决了由一个堆潜在地分配它并在另一个堆上释放它的问题
//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);