Browse Source

Fix false positive exception regarding not-disposing SkPdfTag instances

Marcin Ziąbek 3 months ago
parent
commit
4867075ea4
1 changed files with 17 additions and 3 deletions
  1. 17 3
      Source/QuestPDF/Skia/SkPdfTag.cs

+ 17 - 3
Source/QuestPDF/Skia/SkPdfTag.cs

@@ -83,13 +83,27 @@ internal sealed class SkPdfTag : IDisposable
     {
         if (Instance == IntPtr.Zero)
             return;
-        
-        foreach (var child in Children ?? [])
-            child.Instance = IntPtr.Zero;
+
+        // to dispose the entire tree, it is enough to invoke the pdf_structure_element_delete method on the root element
+        // root's children should be only marked as disposed
+        DisposeChildren(this);
         
         API.pdf_structure_element_delete(Instance);
         Instance = IntPtr.Zero;
         GC.SuppressFinalize(this);
+        
+        static void DisposeChildren(SkPdfTag parent)
+        {
+            if (parent.Children == null)
+                return;
+
+            foreach (var child in parent.Children)
+            {
+                child.Instance = IntPtr.Zero;
+                GC.SuppressFinalize(child);
+                DisposeChildren(child);
+            }
+        }
     }
     
     private static class API