Qimage:内存不足,返回空图像,av_codec_lib



这是我用FFmpeg格式渲染图像的代码,我可以渲染图像,但过了一段时间,由于内存泄漏,我出现了一个错误,QImage:内存不足,返回空图像,我的应用程序崩溃。

SwsContext* img_convert_ctx;
img_convert_ctx = sws_getContext(codecCtx->width,
codecCtx->height,
codecCtx->pix_fmt,
codecCtx->width,
codecCtx->height,
AV_PIX_FMT_RGB24,
SWS_BICUBIC, NULL, NULL, NULL);
AVFrame* frameRGB ;
frameRGB = av_frame_alloc();
avpicture_alloc((AVPicture*)frameRGB,
AV_PIX_FMT_RGB24,
codecCtx->width,
codecCtx->height);
sws_scale(img_convert_ctx,
frame->data,
frame->linesize, 0,
codecCtx->height,
frameRGB->data,
frameRGB->linesize);
QImage image(frameRGB->data[0],
codecCtx->width,
codecCtx->height,
frameRGB->linesize[0],
QImage::Format_RGB888);

如何释放内存?我尝试使用av_frame_free、av_frame_unref,它们被指定为解除分配位于内存的

您使用的QImage构造函数不占用缓冲区的所有权,也不清理av_frame_alloc分配的内存。相反,让QImage管理它的缓冲区,这样就可以清理AVFrame *内存。

frameRGB = av_frame_alloc();
...
QImage image(
codecCtx->width,
codecCtx->height,
frameRGB->linesize[0],
QImage::Format_RGB888);
std::memcpy(image.bits(), frameRGB->data[0], image.sizeInBytes());
av_frame_free(&frameRGB);

最新更新