From 3697494146f854bbf955cc51315b78d844be4083 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Mon, 29 Apr 2024 15:24:16 -0700 Subject: [PATCH] screencopy: Fix reference cycle, that leaks buffer Before this, `/proc/$(pidof cosmic-comp)/maps` quickly expands in length when cosmic-workspaces is opened and closed a bunch of time, preventing the GPU memory from being freed. Which on my Intel system can lead to OOM eventually. There may be other leaks to deal with, but `maps` no longer shows this issue. --- src/wayland/protocols/screencopy.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/wayland/protocols/screencopy.rs b/src/wayland/protocols/screencopy.rs index 54188759..84ed537b 100644 --- a/src/wayland/protocols/screencopy.rs +++ b/src/wayland/protocols/screencopy.rs @@ -23,6 +23,7 @@ use smithay::{reexports::wayland_server::protocol::wl_buffer::WlBuffer, utils::P use smithay::{ reexports::wayland_server::{ protocol::wl_shm, Client, DataInit, Dispatch, DisplayHandle, GlobalDispatch, New, Resource, + Weak, }, utils::Rectangle, }; @@ -418,7 +419,9 @@ struct FrameInner { constraints: Option, buffer: Option, damage: Vec>, - obj: ZcosmicScreencopySessionV2, + // `SessionInner` contains a `Vec`, so use a weak reference here to + // avoid a cycle. + obj: Weak, capture_requested: bool, failed: Option, } @@ -432,7 +435,7 @@ impl FrameInner { constraints: constraints.into(), buffer: None, damage: Vec::new(), - obj, + obj: obj.downgrade(), capture_requested: false, failed: None, } @@ -1019,7 +1022,8 @@ where .known_cursor_sessions .iter() .find(|session| { - session.inner.lock().unwrap().session.as_ref() == Some(&inner.obj) + session.inner.lock().unwrap().session.as_ref() + == inner.obj.upgrade().ok().as_ref() }) .map(|s| CursorSession { obj: s.obj.clone(),