|
| 1 | +.. highlight:: c |
| 2 | + |
| 3 | +.. _life-cycle: |
| 4 | + |
| 5 | +Object Life Cycle |
| 6 | +================= |
| 7 | + |
| 8 | +Stages |
| 9 | +------ |
| 10 | + |
| 11 | +The following is an illustration of the stages of life of an object. Arrows |
| 12 | +indicate a "happens before" relationship. Octagons indicate functions specific |
| 13 | +to :ref:`garbage collection support <supporting-cycle-detection>`. |
| 14 | + |
| 15 | +.. digraph:: callorder |
| 16 | + |
| 17 | + graph [ |
| 18 | + fontname="svg" |
| 19 | + fontsize=10.0 |
| 20 | + layout="dot" |
| 21 | + ranksep=0.25 |
| 22 | + ] |
| 23 | + node [ |
| 24 | + fontname="Courier" |
| 25 | + fontsize=10.0 |
| 26 | + ] |
| 27 | + edge [ |
| 28 | + fontname="Times-Italic" |
| 29 | + fontsize=10.0 |
| 30 | + ] |
| 31 | + |
| 32 | + "start" [fontname="Times-Italic" shape=plain label=< start > style=invis] |
| 33 | + "tp_alloc" [href="typeobj.html#c.PyTypeObject.tp_alloc" target="_top"] |
| 34 | + "tp_new" [href="typeobj.html#c.PyTypeObject.tp_new" target="_top"] |
| 35 | + "tp_init" [href="typeobj.html#c.PyTypeObject.tp_init" target="_top"] |
| 36 | + { |
| 37 | + rank="same" |
| 38 | + "alive" [ |
| 39 | + fontname="Times-Italic" |
| 40 | + label=<alive, ref count > 0> |
| 41 | + shape=box |
| 42 | + ] |
| 43 | + "tp_traverse" [ |
| 44 | + href="typeobj.html#c.PyTypeObject.tp_traverse" |
| 45 | + shape=octagon |
| 46 | + target="_top" |
| 47 | + ] |
| 48 | + } |
| 49 | + "tp_finalize" [ |
| 50 | + href="typeobj.html#c.PyTypeObject.tp_finalize" |
| 51 | + shape=octagon |
| 52 | + target="_top" |
| 53 | + ] |
| 54 | + "tp_clear" [ |
| 55 | + href="typeobj.html#c.PyTypeObject.tp_clear" |
| 56 | + shape=octagon |
| 57 | + target="_top" |
| 58 | + ] |
| 59 | + "ref0" [ |
| 60 | + fontname="Times-Italic" |
| 61 | + label=<ref count == 0> |
| 62 | + ordering="in" |
| 63 | + shape=box |
| 64 | + ] |
| 65 | + "tp_dealloc" [href="typeobj.html#c.PyTypeObject.tp_dealloc" target="_top"] |
| 66 | + "tp_free" [href="typeobj.html#c.PyTypeObject.tp_free" target="_top"] |
| 67 | + |
| 68 | + "start" -> "tp_alloc" |
| 69 | + "tp_alloc" -> "tp_new" |
| 70 | + "tp_new" -> "tp_init" |
| 71 | + "tp_init" -> "alive" |
| 72 | + "tp_traverse" -> "alive" |
| 73 | + "alive" -> "tp_traverse" |
| 74 | + "alive" -> "tp_clear" [label=< cyclic <br/>isolate >] |
| 75 | + "alive" -> "tp_finalize" [ |
| 76 | + dir="back" |
| 77 | + label=< resurrected > |
| 78 | + ] |
| 79 | + "alive" -> "tp_finalize" [label=< cyclic <br/>isolate >] |
| 80 | + "tp_finalize" -> "tp_clear" |
| 81 | + "tp_finalize" -> "ref0" |
| 82 | + "tp_clear" -> "ref0" |
| 83 | + "tp_clear" -> "tp_dealloc" [ |
| 84 | + dir="back" |
| 85 | + label=< optional<br/>direct call > |
| 86 | + ] |
| 87 | + "alive" -> "ref0" |
| 88 | + "ref0" -> "tp_dealloc" |
| 89 | + "tp_finalize" -> "tp_dealloc" [ |
| 90 | + dir="back" |
| 91 | + href="lifecycle.html#c.PyObject_CallFinalizerFromDealloc" |
| 92 | + label=< |
| 93 | + <table border="0" cellborder="0" cellpadding="0" cellspacing="0"> |
| 94 | + <tr> |
| 95 | + <td rowspan="4"> </td> |
| 96 | + <td align="left">optional call to</td> |
| 97 | + <td rowspan="4"> </td> |
| 98 | + </tr> |
| 99 | + <tr> |
| 100 | + <td align="left"><font face="Courier">PyObject_Call</font></td> |
| 101 | + </tr> |
| 102 | + <tr> |
| 103 | + <td align="left"><font face="Courier">FinalizerFrom</font></td> |
| 104 | + </tr> |
| 105 | + <tr><td align="left"><font face="Courier">Dealloc</font></td></tr> |
| 106 | + </table> |
| 107 | + > |
| 108 | + target="_top" |
| 109 | + ] |
| 110 | + "tp_dealloc" -> "tp_free" [label=< directly calls >] |
| 111 | + |
| 112 | +Explanation: |
| 113 | + |
| 114 | +* :c:member:`~PyTypeObject.tp_alloc`, :c:member:`~PyTypeObject.tp_new`, and |
| 115 | + :c:member:`~PyTypeObject.tp_init` are called to allocate memory for a new |
| 116 | + object and initialize the object. |
| 117 | +* If the reference count for an object drops to 0, |
| 118 | + :c:member:`~PyTypeObject.tp_dealloc` is called to destroy the object. |
| 119 | +* :c:member:`~PyTypeObject.tp_dealloc` can optionally call |
| 120 | + :c:member:`~PyTypeObject.tp_finalize` (if non-``NULL``) via |
| 121 | + :c:func:`PyObject_CallFinalizerFromDealloc` if it wishes to reuse that code |
| 122 | + to help with object destruction. |
| 123 | +* :c:member:`~PyTypeObject.tp_finalize` may increase the object's reference |
| 124 | + count, halting the destruction. The object is said to be resurrected. |
| 125 | +* :c:member:`~PyTypeObject.tp_dealloc` can optionally call |
| 126 | + :c:member:`~PyTypeObject.tp_clear` (if non-``NULL``) if it wishes to reuse |
| 127 | + that code to help with object destruction. |
| 128 | +* When :c:member:`~PyTypeObject.tp_dealloc` finishes object destruction, it |
| 129 | + directly calls :c:member:`~PyTypeObject.tp_free` to deallocate the memory. |
| 130 | + |
| 131 | +If the object is marked as supporting garbage collection (the |
| 132 | +:c:macro:`Py_TPFLAGS_HAVE_GC` flag is set in |
| 133 | +:c:member:`~PyTypeObject.tp_flags`), the following stages are also possible: |
| 134 | + |
| 135 | +* The garbage collector occasionally calls |
| 136 | + :c:member:`~PyTypeObject.tp_traverse` to identify :term:`cyclic isolates |
| 137 | + <cyclic isolate>`. |
| 138 | +* When the garbage collector discovers a cyclic isolate, it finalizes one of |
| 139 | + the objects in the group by calling its :c:member:`~PyTypeObject.tp_finalize` |
| 140 | + function. This repeats until the cyclic isolate doesn't exist or all of the |
| 141 | + objects have been finalized. |
| 142 | +* The :c:member:`~PyTypeObject.tp_finalize` function can optionally increase |
| 143 | + the object's reference count, causing it (and other objects it references) to |
| 144 | + become resurrected and no longer a member of a cyclic isolate. |
| 145 | +* When the garbage collector discovers a cyclic isolate and all of the objects |
| 146 | + in the group have already been finalized, the garbage collector clears one of |
| 147 | + the objects in the group by calling its :c:member:`~PyTypeObject.tp_clear` |
| 148 | + function. This repeats until the cyclic isolate doesn't exist or all of the |
| 149 | + objects have been cleared. |
| 150 | + |
| 151 | + |
| 152 | +Functions |
| 153 | +--------- |
| 154 | + |
| 155 | +To allocate and free memory, see :ref:`Allocating Objects on the Heap |
| 156 | +<allocating-objects>`. |
| 157 | + |
| 158 | + |
| 159 | +.. c:function:: void PyObject_CallFinalizer(PyObject *op) |
| 160 | +
|
| 161 | + Calls the object's finalizer (:c:member:`~PyTypeObject.tp_finalize`) if it |
| 162 | + has not already been called. |
| 163 | +
|
| 164 | +
|
| 165 | +.. c:function:: int PyObject_CallFinalizerFromDealloc(PyObject *op) |
| 166 | +
|
| 167 | + Calls the object's finalizer (:c:member:`~PyTypeObject.tp_finalize`) if it |
| 168 | + has not already been called. This function is intended to be called at the |
| 169 | + beginning of the object's destructor (:c:member:`~PyTypeObject.tp_dealloc`). |
| 170 | + The object's reference count must already be 0. If the object's finalizer |
| 171 | + increases the object's reference count, the object is resurrected and this |
| 172 | + function returns -1; no further destruction should happen. Otherwise, this |
| 173 | + function returns 0 and destruction can continue normally. |
0 commit comments