Browse Source

Memory optimization: dispose native objects when the library starts the layout debugging mode

Marcin Ziąbek 9 months ago
parent
commit
8fcf356fbf
1 changed files with 9 additions and 5 deletions
  1. 9 5
      Source/QuestPDF/Drawing/Proxy/LayoutDebugging.cs

+ 9 - 5
Source/QuestPDF/Drawing/Proxy/LayoutDebugging.cs

@@ -132,17 +132,21 @@ internal static class LayoutDebugging
 
 
     public static void RemoveExistingProxies(this Element content)
     public static void RemoveExistingProxies(this Element content)
     {
     {
-        content.VisitChildren(x =>
-        {
-            x.CreateProxy(y => y is ElementProxy proxy ? proxy.Child : y);
-        });
+        content.RemoveExistingProxiesOfType<ElementProxy>();
     }
     }
     
     
     public static void RemoveExistingProxiesOfType<TProxy>(this Element content) where TProxy : ElementProxy
     public static void RemoveExistingProxiesOfType<TProxy>(this Element content) where TProxy : ElementProxy
     {
     {
         content.VisitChildren(x =>
         content.VisitChildren(x =>
         {
         {
-            x.CreateProxy(y => y is TProxy proxy ? proxy.Child : y);
+            x.CreateProxy(y =>
+            {
+                if (y is not TProxy proxy)
+                    return y;
+                
+                (proxy as IDisposable)?.Dispose();
+                return proxy.Child;
+            });
         });
         });
     }
     }