flet icon indicating copy to clipboard operation
flet copied to clipboard

bug: [ft.VideoPlayer] media_kit's glibc leaks memory on GNU/Linux

Open Dejv311 opened this issue 9 months ago • 2 comments

Duplicate Check

  • [x] I have searched the opened issues and there are no duplicates

Describe the bug

The bug is documented and described in this thread:

https://github.com/media-kit/media-kit/issues/68

When dismissing the VideoPlayer control, the underlying media_kit package does not free up the memory. The attached thread solves this issue by using a different memory allocation library; see answer https://github.com/media-kit/media-kit/issues/68#issuecomment-1666600498.

This is probably related to the following open issue: https://github.com/flet-dev/flet/issues/6106

Code sample

import asyncio
import warnings

import flet as ft
import flet_video as fv

DELAY_S = 5

VIDEO_A = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerJoyrides.mp4"
VIDEO_B = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerMeltdowns.mp4"

class SwapView(ft.View):
    def __init__(self, route: str, next_route: str, bgcolor: str, video_url: str):
        super().__init__(route=route, bgcolor=bgcolor, padding=20)

        self._next_route = next_route
        self._running = False
        self._task = None

        self.video = fv.Video(
            playlist=[fv.VideoMedia(resource=video_url)],
            playlist_mode=fv.PlaylistMode.SINGLE,
            autoplay=True,
            muted=True,
            fit=ft.BoxFit.CONTAIN,
            show_controls=True,
        )

        self.controls = [
            ft.Text(f"Route: {route}", size=20, weight=ft.FontWeight.BOLD),
            ft.Text(f"Auto-switch to: {next_route} in {DELAY_S}s"),
            ft.Row(
                [
                    ft.Button("Go /a", on_click=lambda e: self.page.go("/a")),
                    ft.Button("Go /b", on_click=lambda e: self.page.go("/b")),
                ]
            ),
            ft.Container(height=360, bgcolor=ft.Colors.BLACK, padding=10, content=self.video),
        ]

    def did_mount(self):
        self._running = True
        self._task = self.page.run_task(self._auto_switch)

    def will_unmount(self):
        self._running = False
        if self._task:
            try:
                self._task.cancel()
            except Exception:
                pass

    async def _auto_switch(self):
        await asyncio.sleep(DELAY_S)
        if self._running and self.page:
            self.page.go(self._next_route)


def main(page: ft.Page):
    page.title = "Video memory leak test"
    page.padding = 0

    def route_change(e: ft.RouteChangeEvent):
        page.views.clear()

        if page.route == "/b":
            page.views.append(SwapView("/b", "/a", ft.Colors.GREEN_200, VIDEO_B))
        else:
            page.views.append(SwapView("/a", "/b", ft.Colors.BLUE_200, VIDEO_A))

        page.update()

    page.on_route_change = route_change
    page.go("/a")


ft.run(main)

To reproduce

Dismiss VideoPlayer control repeatedly.

Expected behavior

No response

Screenshots / Videos

No response

Operating System

Linux

Operating system details

Ubuntu 25.10

Flet version

0.80.5

Regression

No, it isn't

Suggestions

No response

Logs

Additional details

No response

Dejv311 avatar Feb 13 '26 13:02 Dejv311

Any resolution?

HappyCoder256 avatar Mar 01 '26 14:03 HappyCoder256

Any resolution?

this issue I have encountered last year, and seem only can be fixed if you run build, but I haven't tried it yet looking forward to normal run won't be leeked https://github.com/flet-dev/flet-video/issues/5#issue-2890118130

viethoang261 avatar Apr 07 '26 11:04 viethoang261