From 783315bbad4623d03f5f4887b05efcca1c35387d Mon Sep 17 00:00:00 2001 From: Christian Luksch Date: Tue, 9 Jul 2024 17:08:28 +0200 Subject: [PATCH] [GL] fixed memory leak caused by missing onMakeCurrent callbacks when ContextHandle is disposed (destroy of UnsharedObjects/Framebuffer) --- src/Aardvark.Rendering.GL/Core/ContextHandles.fs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Aardvark.Rendering.GL/Core/ContextHandles.fs b/src/Aardvark.Rendering.GL/Core/ContextHandles.fs index dc8957e24..e2419562e 100644 --- a/src/Aardvark.Rendering.GL/Core/ContextHandles.fs +++ b/src/Aardvark.Rendering.GL/Core/ContextHandles.fs @@ -86,6 +86,7 @@ type ContextHandle(handle : IGraphicsContext, window : IWindowInfo) = (handle |> unbox).GetAddress(name) member x.OnMakeCurrent(f : unit -> unit) = + if isDisposed then failwith "Failed to register OnMakeCurrent callback, the context is already disposed!" Interlocked.CompareExchange(&onMakeCurrent, ConcurrentHashSet(), null) |> ignore onMakeCurrent.Add f |> ignore @@ -214,6 +215,15 @@ type ContextHandle(handle : IGraphicsContext, window : IWindowInfo) = if lockTaken then if not isDisposed then + + // release potentially pending UnsharedObjects + let actions = Interlocked.Exchange(&onMakeCurrent, null) + if actions <> null then + x.Use(fun () -> + for a in actions do + a() + ) + isDisposed <- true debugOutput |> Option.iter (fun dbg -> dbg.Dispose()) onDisposed.Trigger()