【发布时间】:2012-10-10 23:39:12
【问题描述】:
我正在为Windows 使用libavcodec,而avformat_open_input() 似乎有严重的内存泄漏。如果我打开 5,000 个视频,操作系统会报告应用程序退出时未释放的 2 GB RAM 消耗。代码如下:
AVFormatContext *pFormatCtx = NULL;
AVDictionary *dict = NULL;
int result = 0;
av_register_all();
// open the input video file
IntPtr ip = Marshal::StringToHGlobalAnsi(videoFilename);
const char* filename = static_cast<const char*>(ip.ToPointer());
result = avformat_open_input(&pFormatCtx, filename, NULL, &dict);
if (result < 0) {
Marshal::FreeHGlobal(ip);
return result;
}
Marshal::FreeHGlobal(ip);
avformat_close_input(&pFormatCtx);
return result;
以上代码位于从 C# 调用的类库中。我正在使用托管 C++ 来调用 libavcodec 库。流程是 C# -> 托管 C++ -> libavcodec。我正在使用 DLL 和动态链接。这是一个单线程应用程序。当我按预期使用线程时,泄漏会增加。
我尝试了以下方法:
- 我已经尝试了几个 32 位版本并且内存泄漏是一致的。
- 使用
NULL代替&dict。 - 使用相同文件名调用
avformat_open_input()5,000+次不会泄漏内存。 - 使用
avformat_alloc_context()和avformat_free_context()的组合。我找不到可以释放内存的组合。
【问题讨论】:
标签: visual-c++ memory-leaks visual-studio-2012 libavcodec libavformat