Use vs FFmpeg diagnostic tool to detect memory leaks

A brief:    

    Before blogger blog " calling av_read_frame FFmpeg function caused memory leaks " in about a memory leak problem in which an improper use of FFmpeg's api. But the memory leaks caused by improper use of FFmpeg api there are many, there are many functions such as FFmpeg application space, free space if the corresponding function is not called after the application space, will cause memory leak. That we are not very familiar with the case FFmpeg function of how to find the cause of a memory leak that caused it? We can use some tools to help check. Memory leak detection tools under Windows there are several, such as vld (visual leak detector), WinDbg , vs diagnostic tools. Which vld only at the end of the program to show the cause of memory leaks, WinDbg operation was too complicated, so here vs bloggers recommend the use of diagnostic tools. For the development of the program vs, vs using diagnostic tools to help check the memory leaks, and there take a snapshot of a function, the program is running, you can find the cause of memory leaks. Below vs2015 example, explain its use.

 

Second, download the configuration of static libraries FFmpeg

    vs diagnostic tool can only locate the executable file (exe) and memory leaks inside the static library can not locate dynamic library (dll) inside the issue. So if you want to call detect memory leaks caused by FFmpeg the api, we have to use static library FFmpeg, FFmpeg can not use dynamic libraries. FFmpeg official only provide its source code and dynamic libraries, static libraries have to compile it yourself, if you do not want to compile your own here, from Bowen " FFmpeg Windows versions static library download Static Library". Once downloaded vs we can create a new project, add a static library of the FFmpeg. We "Bowen calls memory leaks caused by FFmpeg in av_read_frame function " in the program as an example to explain how to use vs diagnostic tool. Procedure as follows:

main.cpp

#include <stdio.h>
#include <iostream>
#include <windows.h>
 
extern "C" {
#include "libavformat/avformat.h"
}
 
int main()
{
	const char *path = "video1.mp4"; //需要读取的本地媒体文件相对路径为video1.mp4
	av_register_all();               //初始化所有组件,只有调用了该函数,才能使用复用器和编解码器。否则,调用函数avformat_open_input会失败,无法获取媒体文件的信息
	avformat_network_init();         //打开网络流。这里如果只需要读取本地媒体文件,不需要用到网络功能,可以不用加上这一句
	AVDictionary *opts = NULL;
	AVFormatContext *ic = NULL;
	int re = avformat_open_input(&ic, path, NULL, &opts); //媒体打开函数,调用该函数可以获得路径为path的媒体文件的信息,并把这些信息保存到指针ic指向的空间中(调用该函数后会分配一个空间,让指针ic指向该空间)
	if (re != 0) //如果打开媒体文件失败,打印失败原因
	{
		char buf[1024] = { 0 };
		av_strerror(re, buf, sizeof(buf) - 1);
		printf("open %s failed!:%s", path, buf);
	}
	else  //打开媒体文件成功
	{
		printf("open %s success!\n", path);
		avformat_find_stream_info(ic, NULL); //调用该函数可以进一步读取一部分视音频数据并且获得一些相关的信息
		av_dump_format(ic, 0, path, 0);      //输出媒体文件的信息到控制台上
 
		AVPacket *pkt = av_packet_alloc();
		while (1)
		{
			int ret = av_read_frame(ic, pkt);
			if (ret != 0)
			{
				printf("\n--------------------------end--------------------------");
				break;
			}
			//av_packet_unref(pkt);
			Sleep(2);  //睡眠2ms,避免程序执行过快影响观察效果。
		}
		av_packet_free(&pkt);
	}
	
	if (ic)
	{
		avformat_close_input(&ic);
	}
	getchar();
	return 0;
}

 

In our new vs a Win32 console application, add the above main.cpp, and then added in FFmpeg vs static libraries (such as of v3.4.2 FFmpeg static library), as follows:

 

 

 

After adding a static library is complete, we compile the program so that the program can "According to Bowen calls the function av_read_frame memory leaks caused by FFmpeg in the" Run as the memory leak problem.

 

Third, the use of FFmpeg vs diagnostic tool to detect memory leaks

We press the "F5" shortcut keys vs in -> "Start Debugging", the interface will appear as shown in the following figure:

 

We then press the "take a snapshot" button vs diagnostic tool in two snapshots taken during the program run, as shown below, it can be seen to increase the second snapshot memory than the first snapshot.

 

Click the left mouse button a second snapshot, to highlight it, then click the "View the heap." 

 

After performing the above operations, the page will appear as shown in the FIG. In "By comparison with 'here to choose a" snapshot # 1 "," View Mode "Pick" type view. " This will display a snapshot of the increased memory size 2 to 1 snapshot. We can see that the difference is 5340933 bytes in size "unresolved allocation" line,

 

Then double-click on "unresolved allocation" line.

 

In the interface shown in FIG appears, double-click on a row.

 

Select "ConsoleApplication2.exe main () -! Line 32" line, click the right mouse button, click on the "Go to the source code" in the context menu that appears.

 

Then we can see vs automatically help us navigate to the "int ret = av_read_frame (ic, pkt);" line, and that line is the cause of program memory leaks.

 

So far, we have learned how to use the FFmpeg vs diagnostic tool to detect memory leaks problems.

Published 54 original articles · won praise 55 · views 120 000 +

Guess you like

Origin blog.csdn.net/u014552102/article/details/103865695