mpv
mpv copied to clipboard
libmpv with lua: leaking memory with each new mpv instance
mpv Information
[ 0.003][v][cplayer] mpv v0.38.0-559-g22fc2e6a Copyright © 2000-2024 mpv/MPlayer/mplayer2 projects
[ 0.004][v][cplayer] built on Jun 26 2024 12:07:11
[ 0.004][v][cplayer] libplacebo version: v7.349.0 (v7.349.0-rc1-3-g1fd3c7b-dirty)
[ 0.004][v][cplayer] FFmpeg version: N-116017-ge61fed828
[ 0.004][v][cplayer] FFmpeg library versions:
[ 0.004][v][cplayer] libavcodec 61.8.100
[ 0.004][v][cplayer] libavdevice 61.2.100
[ 0.004][v][cplayer] libavfilter 10.2.102
[ 0.004][v][cplayer] libavformat 61.4.100
[ 0.004][v][cplayer] libavutil 59.26.100
[ 0.004][v][cplayer] libswresample 5.2.100
[ 0.004][v][cplayer] libswscale 8.2.100
[ 0.004][v][cplayer]
[ 0.004][v][cplayer] Configuration: -Dgpl=false -Ddebug=true -Db_ndebug=true -Doptimization=3 -Db_lto=true -Db_lto_mode=thin -Dcplayer=false -Dlibmpv=true -Dpdf-build=disabled -Dlua=enabled -Djavascript=enabled -Dsdl2=enabled -Dlibarchive=enabled -Dlibbluray=enabled -Ddvdnav=disabled -Duchardet=enabled -Drubberband=disabled -Dlcms2=enabled -Dopenal=disabled -Dspirv-cross=enabled -Dvulkan=enabled -Dvapoursynth=enabled -Degl-angle=enabled -Dc_args=-Wno-error=int-conversion -Dprefix=/__w/mpv-winbuild/mpv-winbuild/mpv-winbuild-cmake/build64/install/x86_64-w64-mingw32 -Dlibdir=/__w/mpv-winbuild/mpv-winbuild/mpv-winbuild-cmake/build64/install/x86_64-w64-mingw32/lib -Ddefault_library=shared -Dprefer_static=True --cross-file=/__w/mpv-winbuild/mpv-winbuild/mpv-winbuild-cmake/build64/meson_cross.txt
[ 0.004][v][cplayer] List of enabled features: build-date cplugins cuda-hwaccel cuda-interop d3d-hwaccel d3d11 d3d9-hwaccel debug dos-paths dxgi-debug dxgi-debug-d3d11 egl-angle egl-angle-win32 ffmpeg ffnvcodec gl gl-dxinterop gl-dxinterop-d3d9 gl-win32 glob glob-win32 iconv javascript jpeg lcms2 libarchive libass libavdevice libbluray libplacebo luajit pathcch sdl2 sdl2-audio sdl2-gamepad sdl2-video shaderc spirv-cross uchardet vaapi vaapi-win32 vapoursynth vector vulkan vulkan-interop wasapi win32 win32-desktop win32-threads zimg zimg-st428 zlib
(https://github.com/zhongfly/mpv-winbuild/releases/tag/2024-06-26-22fc2e6)
Other Information
- Windows version: Windows 11 23H2 (10.0.22631.3737)
- Source of mpv: Recent libmpv Windows builds by shinchiro and zhongfly; self-compiled with MSYS2 as described here
- Introduced in version: Possibly between 2020-04-05 and 2020-04-12?
Reproduction Steps
I'm seeing an issue where creating and terminating an mpv instance with libmpv leaks some memory (~1.5 MB per instance) if lua is enabled.
I believe this issue was previously discussed in #9860.
I'm using this test program linked against the libmpv DLL:
int main()
{
std::cin.ignore(10000, '\n');
std::cout << "Starting\n";
for (int i = 0; i < 500; i++)
{
auto handle = mpv_create();
mpv_initialize(handle);
mpv_terminate_destroy(handle);
}
std::cout << "Finished\n";
std::cin.ignore(10000, '\n');
}
I would expect memory usage to remain constant, but it increases linearly while the loop is running and never goes down again.
I was able to narrow it down to the lua build option: If I disable lua from the mpv build (-Dlua=disabled), the memory leak disappears, and memory use is constant when using the test program.
I was using the builds and scripts by shinchiro and zhongfly initially, then tried compiling a build with MSYS2 as described here and I'm seeing the same behavior with that.
I can confirm this post stating that the shinchiro libmpv build from 2020-04-05 did not leak, but the build from 2020-04-12 did, so it could be that the problem was introduced around that time.
Expected Behavior
No memory leak with lua enabled.
Actual Behavior
Memory leak with lua enabled.
Log File
Added mpv_load_config_file(handle, "log.conf"); after mpv_create(), log.conf contains this:
log-file=mpv-log.txt
msg-level=all=debug
gpu-debug=yes
Log file for one iteration: mpv-log.txt
Sample Files
No response
I carefully read all instruction and confirm that I did the following:
- [X] I tested with the latest mpv version to validate that the issue is not already fixed.
- [X] I provided all required information including system and mpv version.
- [X] I produced the log file with the exact same set of files, parameters, and conditions used in "Reproduction Steps", with the addition of
--log-file=output.txt. - [X] I produced the log file while the behaviors described in "Actual Behavior" were actively observed.
- [X] I attached the full, untruncated log file.
- [X] I attached the backtrace in the case of a crash.
Without bothering to check if there's any leak for me, here are the related looking commits in that timeframe if anyone wants to look into it.
fd3caa264ea0848e7e30db94390063c87e247003 1e780251aebc4103242cf60d9cba6aa2f6424f62 a67bda28409dd893617ef47f6e089fd753d7de78
i opened several issues on this subject ... the problem is real and you always close without investigations ... it leaks approx 1 MB at each instance
#9860
Oh so actually I can confirm this happens but it's only with luajit. That would explain why people in the old issue couldn't reproduce it reliably.
Does it also happen if you compile luajit with -DLUAJIT_USE_SYSMALLOC, they have their own allocator. I don't know the details, but maybe it doesn't release memory right away, and this is what you see.
maybe it doesn't release memory right away
Did not try messing with luajit yet, but this is almost certainly what is happening. ASAN doesn't report any leaks.
is this related to our topic? https://github.com/LuaJIT/LuaJIT/issues/1179
is this related to our topic?
Unfortunately I am using luajit from git master which includes the fix for that issue and the problem still occurs.
Does it also happen if you compile luajit with -DLUAJIT_USE_SYSMALLOC,
~Unless I'm doing it wrong, yeah it still happens with this.~ My bad, wrong compile. It indeed doesn't happen with that flag.
is it possible to use the flag -DLUAJIT_USE_SYSMALLOC with the official win release of libmpv? do we have to ask it to shinchiro?
Will be fixed by #14457
hello, could you please reopen this issue?
#14457 has fixed the memory leak but after this commit there is 1 leak handle at each new mpv instance (that was not present before the #14457 )
@kasper93 could you please check if in your commit there is an handle that is not closed?
Thanks for the quick fix! Can confirm that the leak is gone in the latest zhongfly build.