Multi-Threaded Video Decoder Leaks Memory

2019-06-09 11:09发布

问题:

My intention is to create a relatively simple video playback system to be used in a larger program that I am working on. Relevant code to the video decoder is here. The best I have been able to do so far is narrow down the memory leak to this section of code (or rather I have not noticed any memory leaks occurring when video is not used).

This is probably a very broad question how ever I am unsure of the scope of the issue I am having and as such of how to word my question.

What I want to know is what have I missed or done wrong that has lead to a noticeable memory leak (by noticeable I mean I can watch memory usage climb megabytes per minute). I have tried ensured that every allocation that I make is matched by a deallocation.

EDIT 1
This is to be built on a Windows 10 machine running MSYS2 (MinGW64)

回答1:

Best way to catch a leak is to use in built memory leak checker on compiler, way better than valgrind if you can compile with it.

Add this line to your makefile:

CXXFLAGS += -fno-omit-frame-pointer -fsanitize=address -ggdb3 -O0

If you use old version of gcc (which doesn't support memory sanitizer/checker use clang).

What I know is av_packet_unref may not work as expected if certain conditions not met (you can read more on inside ffmpeg source codes, can't remember the name it was one of the header files talks about these).

After compilation, let memory climb noticeable, then use Ctrl+C to quit. Memory sanitizer will dump the function/line where memory allocated (and not freed later) automatically. If you can't see source code line numbers, use addr2line tool.

Hope that helps.