From 48fa1d7ddffc0418cfa65215d7be86ba469698de Mon Sep 17 00:00:00 2001 From: yunchan Date: Sun, 11 Oct 2026 15:34:27 +0900 Subject: [PATCH] linux: free frame buffers on closeTexture and in finalize (Kitedesk B212) closeTexture marked the texture terminated but kept the frame last handed to the engine and any frame still waiting, and TextureRgba had no finalize, so each closed session kept one or two frame-sized buffers forever (2 x width x height x 4 bytes measured per 1080p session). terminate now frees both buffers under the texture mutex: the texture is already unregistered and OnRgba stops copying once terminated is set. A finalize frees them as well. GObject and GL lifetime are unchanged here. Tests: close_frees_frame_buffers, closed_texture_ignores_late_frames, finalize_frees_frame_buffers and frames_racing_close pass (also under ASan); the four recycling tests still fail. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../texture_rgba_renderer.h | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/linux/include/texture_rgba_renderer/texture_rgba_renderer.h b/linux/include/texture_rgba_renderer/texture_rgba_renderer.h index 3bdaae0..f2e17a7 100644 --- a/linux/include/texture_rgba_renderer/texture_rgba_renderer.h +++ b/linux/include/texture_rgba_renderer/texture_rgba_renderer.h @@ -1,3 +1,6 @@ +// Modified by the Kitedesk fork (2026, B212): frame buffers are freed when the +// texture is closed and when the object is finalized. + #ifndef TEXTURE_RGBA_RENDERER_H #define TEXTURE_RGBA_RENDERER_H @@ -40,9 +43,26 @@ G_DEFINE_TYPE(TextureRgba, texture_rgba, TextureRgba)) +// Frees the frame waiting for the next raster frame and the frame last handed +// to the engine. Call with the mutex held, or when no other thread can use the +// texture any more. +static void texture_rgba_free_buffers(TextureRgba* self) { + if (g_atomic_int_get(&self->buffer_ready)) { + // Not handed to the engine yet; prior_buffer is the older frame. + delete[] self->buffer; + } + delete[] self->prior_buffer; + self->buffer = nullptr; + self->prior_buffer = nullptr; + g_atomic_int_set(&self->buffer_ready, FALSE); +} + static void texture_rgba_terminate(TextureRgba* self) { g_mutex_lock(&self->mutex); g_atomic_int_set(&self->terminated, TRUE); + // The texture is unregistered, so the engine reads neither frame again, and + // OnRgba stops copying frames once terminated is set. + texture_rgba_free_buffers(self); g_mutex_unlock(&self->mutex); } @@ -137,8 +157,16 @@ static TextureRgba *texture_rgba_new(FlTextureRegistrar* registrar) return texture; } +static void texture_rgba_finalize(GObject* object) { + TextureRgba* self = TEXTURE_RGBA(object); + texture_rgba_free_buffers(self); + g_mutex_clear(&self->mutex); + G_OBJECT_CLASS(texture_rgba_parent_class)->finalize(object); +} + static void texture_rgba_class_init(TextureRgbaClass *klass) { + G_OBJECT_CLASS(klass)->finalize = texture_rgba_finalize; FL_PIXEL_BUFFER_TEXTURE_CLASS(klass)->copy_pixels = texture_rgba_copy_pixels; }