You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Make it more obvious that closing parents explicitly while having
children closed automatically is against the API contract, at least for
the time being.
PR #224 tries to change this and handle child closing automatically.
Copy file name to clipboardExpand all lines: docs/source/python_api.rst
+10-4Lines changed: 10 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -44,14 +44,22 @@ Memory management
44
44
.. Information on PDFium's behaviour: https://groups.google.com/g/pdfium/c/7qLFskabmnk/m/xQEnXiG5BgAJ
45
45
.. Limitations of weakref: https://stackoverflow.com/q/52648418/15547292/#comment131514594_58243606
46
46
47
+
.. note::
48
+
This section covers the support model, which does a lot of protective handling around raw pdfium close functions.
49
+
It is not applicable to the raw API alone!
50
+
47
51
PDFium objects commonly need to be closed by the caller to release allocated memory. [#ac_obj_ownership]_
48
52
Where necessary, pypdfium2's helper classes implement automatic closing on garbage collection using :class:`weakref.finalize`. Additionally, they provide ``close()`` methods that can be used to release memory explicitly.
49
53
50
54
It may be advantageous to close objects explicitly instead of relying on Python garbage collection behaviour, to release allocated memory and acquired file handles immediately. [#ac_obj_opaqueness]_
51
-
In this case, note that objects must be closed in reverse order to loading (i. e. parent objects must never be closed before child objects), and that closed objects must not be accessed anymore. [#ac_mixed_closing]_
52
-
55
+
In this case, note that objects must be closed in reverse order to loading (i. e. parent objects must never be closed before child objects).
53
56
If the order of closing is violated, pypdfium2 issues a warning, but still makes an unsafe attempt to close the child object.
57
+
58
+
In particular, when closing parent objects explicitly, this means that any spawned child objects ought to be closed explicitly as well, since child closing on garbage collection threatens to happen later than explicit parent closing.
59
+
For example, if you have a document and derived pages, you may either do both ``page.close()`` and ``pdf.close()``, or ``page.close()`` alone, or omit close calls entirely, but you **must not** call ``pdf.close()`` without prior ``page.close()``.
60
+
54
61
``close()`` methods should be called only once. Excessive close calls are skipped with a warning.
62
+
Closed objects must not be accessed anymore.
55
63
Moreover, closing an object sets the underlying ``raw`` attribute to None, which should ideally turn illegal function calls after ``close()`` into a no-op.
56
64
57
65
It is important to note that raw objects must never be isolated from their wrappers. Continuing to use a raw object after its wrapper has been closed or garbage collected is bound to result in a use after free scenario.
@@ -61,8 +69,6 @@ Due to limitations in :mod:`weakref`, finalizers can only be attached to wrapper
61
69
62
70
.. [#ac_obj_opaqueness] Python does not know how many resources an opaque C object might bind.
63
71
64
-
.. [#ac_mixed_closing] Consequently, if closing an object explicitly, all child objects must be closed explicitly as well. Otherwise, they may not have been garbage collected and finalized before the explicit close call on the parent object, resulting in an illicit order of closing.
0 commit comments