macos: let closed textures' buffers go, keep closed identities (Kitedesk T-4204)

closeTexture unregistered the texture but kept it in the plugin's map with
its last frame (33 MB at 4K) until the engine went away, and the texture
went on taking and announcing frames pushed to its pointer. Kitedesk makes a
texture per display, so each display switch and closed tab leaked a frame.

closeTexture now takes the texture out of the map and closes it: frames
pushed afterwards are dropped and not announced, the engine gets no frame any
more, and the frames, the handed buffers and the pool go. As on Windows
(B252), a texture closed without "recycle" may still get frames through the
pointer from getTexturePtr (a tab moving to another window keeps its
session's pointer), so it is kept for the process lifetime and its address
is never another texture's; with "recycle" it is freed once the engine has
unregistered it. The textures still open when the engine goes away are closed
and kept the same way: the registry's reference to them used to keep them
alive, announcing every frame to a registry without an engine. A closed
texture no longer holds the registry.

Tests: closing_frees_frame_buffers (634 MB more after 20 4K textures, now
0.2 MB), closed_texture_drops_late_frames,
closing_keeps_only_unrecycled_textures, open_textures_outlive_their_engine
and frames_racing_close pass: 13 of 13.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
yunchan 2026-11-15 09:52:18 +09:00
parent 2a36975b62
commit 6c3425539f
3 changed files with 55 additions and 11 deletions

View file

@ -1,3 +1,7 @@
// Modified by the Kitedesk fork (2026, T-4204): closeTexture lets the
// texture's buffers go, and keeps the closed texture for good unless "recycle"
// is set; the textures of an engine going away are kept the same way.
import Cocoa
import FlutterMacOS
@ -5,6 +9,12 @@ public class TextureRgbaRendererPlugin: NSObject, FlutterPlugin {
private var renderer: Dictionary<Int64, TextRgba> = [:]
private var texture_registry: FlutterTextureRegistry?
// Textures closed while frames may still be pushed to their pointer from getTexturePtr:
// closeTexture without "recycle" (a tab moving to another window keeps its session's
// pointer), or an engine going away first. They are never freed, so such a frame finds a
// closed texture, not freed memory, and their address is never another texture's (ABA).
// Each keeps the object, not its buffers. Main thread only.
private static var retired: [TextRgba] = []
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "texture_rgba_renderer", binaryMessenger: registrar.messenger)
@ -12,6 +22,14 @@ public class TextureRgbaRendererPlugin: NSObject, FlutterPlugin {
instance.setTextureRegistry(registry: registrar.textures)
registrar.addMethodCallDelegate(instance, channel: channel)
}
deinit {
// The engine is going away, and sessions may still push frames to these textures.
for textRgba in renderer.values {
textRgba.close()
TextureRgbaRendererPlugin.retired.append(textRgba)
}
}
public func setTextureRegistry(registry: FlutterTextureRegistry) {
texture_registry = registry
@ -34,9 +52,15 @@ public class TextureRgbaRendererPlugin: NSObject, FlutterPlugin {
case "closeTexture":
let args = call.arguments as! Dictionary<String, Any?>
let key = args["key"] as! Int64
if (renderer[key] != nil) {
if (-1 != renderer[key]?.textureId) {
texture_registry?.unregisterTexture(renderer[key]?.textureId ?? -1)
// "recycle": true promises that nothing pushes frames to the pointer any more.
let recycle = args["recycle"] as? Bool ?? false
if let textRgba = renderer.removeValue(forKey: key) {
textRgba.close()
if (-1 != textRgba.textureId) {
texture_registry?.unregisterTexture(textRgba.textureId)
}
if (!recycle) {
TextureRgbaRendererPlugin.retired.append(textRgba)
}
result(true)
} else {