我已经实现了一个类,它生成一个用于读取帧和对帧进行排队的线程,并且主线程通过 OpenGL 显示这些帧。我尝试在将图像数据绑定到 OpenGL 纹理后释放分配的内存,但似乎有些内存未正确释放。内存使用量不断增长,直到系统内存耗尽,最终帧读取器线程由于内存分配失败而无法抓取新帧。有人可以帮助我解决我可能错过的事情吗?谢谢。
这是帧读取器线程的代码:
void AVIReader::frameReaderThreadFunc()
{
AVPacket packet;
while (readFrames) {
// Allocate necessary memory
AVFrame* pFrame = av_frame_alloc();
if (pFrame == nullptr)
{
continue;
}
AVFrame* pFrameRGB = av_frame_alloc();
if (pFrameRGB == nullptr)
{
av_frame_free(&pFrame);
continue;
}
// Determine required buffer size and allocate buffer
int numBytes = avpicture_get_size(AV_PIX_FMT_RGB24, pCodecCtx->width,
pCodecCtx->height);
uint8_t* buffer = (uint8_t *)av_malloc(numBytes * sizeof(uint8_t));
if (buffer == nullptr)
{
av_frame_free(&pFrame);
av_frame_free(&pFrameRGB);
continue;
}
// Assign appropriate parts of buffer to image planes …Run Code Online (Sandbox Code Playgroud)