c++ - AVFrame 到 QImage 内存泄漏

标签 c++ qt memory-leaks ffmpeg

我正在尝试将视频帧解码为 QImage 并显示它,但是下面将 AVFrame 转换为 QImage 的代码导致内存泄漏。当我禁用下面的代码时,应用程序工作正常,但会导致使用更多的 RAM 和时间。

    AVFrame *frameRGB = av_frame_alloc();
    int width = frame->width, height = frame->height;
    avpicture_alloc( ( AVPicture *) frameRGB, AV_PIX_FMT_RGB24, width,height);

    struct SwsContext *convert_ctx=NULL;
    enum PixelFormat src_pixfmt = (enum PixelFormat)frame->format;
    enum PixelFormat dst_pixfmt = PIX_FMT_RGB24;
    convert_ctx = sws_getContext(width, height, src_pixfmt, width, height, dst_pixfmt,
    SWS_FAST_BILINEAR, NULL, NULL, NULL);
    sws_scale(convert_ctx,frame->data, frame->linesize,0,height, frameRGB->data, frameRGB->linesize);

    //decodedQimage = QImage( width, height, QImage::Format_RGB888 );
    for( int y = 0; y < height; ++y ){
       memcpy( decodedQimage.scanLine(y), frameRGB->data[0]+y * frameRGB->linesize[0],  frameRGB->linesize[0] );
    }

   av_free(frameRGB);
   sws_freeContext(convert_ctx);

这里 decodedQimage 是 QImage 类型并且像 int 一样

        decodedQimage = QImage( outputwidth, outputheight, QImage::Format_RGB888 );

最佳答案

使用av_frame_free() ,而不是 av_free(),以释放 frameRGB

关于c++ - AVFrame 到 QImage 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38069014/

相关文章:

c++ - 将 Armadillo 函数传递给用户定义的函数

c++ - 如何使用 C++ 在 CSV 文件上执行逐行操作(一些 x)

c++ - QFileDialog动态翻译

qt - 如何使用 QPainter 绘制和填充三角形?

java - Android ArrayList 内存泄漏

java - SVNKit 内存泄漏 : SVNLog. run() 创建永远不会被杀死的线程

c++ - __tg_promote 在 tgmath.h 中做什么

c++ - Clang 3.1 和 C++11 支持状态

c++ - ShellExecuteEx runas 中的 lpParameters

c++ - 你能给我一个内存泄漏的真实工作示例吗?