Memory Leak in Video Player

Asked

Viewed 62 times

1

I’m implementing an application in C++/cli and Winforms that features videos continuously. During execution, when videos are loaded the memory increases and is released later. I’ve noticed that after a while, the memory used goes up. After a full day of execution the application is terminated by having this memory leak.

I have already isolated the player from the remaining components in order to verify that this was the problem. I’ve tried some players - Mediaplayer Component, Activex VLC, Videolandotnet or Directx SDK - but the problem remains sometimes not so obvious but always ends up happening.

Someone had a similar problem ? It is necessary to stop or start some service for the operating system to allow these operations?

1 answer

2


There are basically two reasons for addressing space to go only increasing: memory leakage and memory fragmentation.

In the leak not much to do besides running tools like cppcheck and valgrind so that they can identify the leaks.

The cppcheck will perform static source code analysis, and will identify errors such as bad programming practices, allocated variables leaving the scope without being out of focus, etc. However, it is not very accurate since it only performs static analysis, but has the advantage of being faster.

Already the valgrind will analyze all memory allocations, performing the application’s Profiling in the background, and will point out all memory leaked in the application.

If they spot a mistake, they will give you information that will help you solve the problem (although they are not always clear).

In the case of memory fragmentation, what happens is the following: memory is not allocated in small pieces (either by the OS or by the Runtime being used), but in memory pages (usually with at least 4KB), and then divided into smaller pieces.

However, if there is a page with only 4 bytes occupied, and you want to occupy other 4KB, it is not possible to take advantage of the page that is practically free, but rather to allocate a new page. Note that it formed a "hole" in one of the pages that was not used, but that in this case it is large enough to be used again in an allocation in the course of the program.

In some cases, however, the "hole" is too small and it turns out to be impossible to allocate it for any useful use. Of course, this depends on how the program was developed, whether it usually displaces large chunks of memory, or whether memory is allocated indefinitely, among other factors. But the important thing is that this can cause the program to allocate more and more memory, although it’s not exactly leaking, but just fragmenting.

One way to avoid these problems is to allocate as many variables as possible to the stack. Thus, when they go out of scope, they are automatically released, avoiding memory Leak. In addition, the stack is a region with always contiguous addresses and allocated in sequence, and therefore there is no memory fragmentation, besides having faster access to the heap.

Some components only displace memory when the entire component is out of focus, i.e., imagine that you created the player and are playing videos on it. However, it was not so well written, and it ends up saving some (or several) bytes between a video and another instead of releasing them. However, programmers took care to release the memory when the component is destroyed. An option would then be at the end of each video to recreate the component (or each 2 videos, depending on your needs). Oddly enough, I’ve seen it happen.

Anyway, the ideal solution is to find the leak and fix it. Also, if in all these components the leak occurs, analyze if it is not occurring in your code instead of the other code. It may be that the error is very subtle, but with tools like the valgrind the error can be identified.

I hope I’ve helped you with something, and I’m sorry for the long text.

  • 1

    Thank you very much for your attention and reply!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.