This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Uninitialized objects are tracked by the garbage collector
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.6, Python 3.2, Python 3.3, Python 3.4, Python 3.5
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: BTaskaya, h.venev, pablogsal
Priority: normal Keywords:

Created on 2015-04-04 15:00 by h.venev, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg240079 - (view) Author: Hristo Venev (h.venev) * Date: 2015-04-04 15:00
An object starts being tracked by the GC after being allocated, but before being initialized. If during initialization the GC runs, this may lead to tp_traverse being called on an uninitialized object.
msg358729 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2019-12-20 19:11
If I understand what you say correctly, it does not seem correct, the object will only be tracked by the collector when you call PyObject_GC_Track. indeed, the documentation warns about this:

void PyObject_GC_Track(PyObject *op)¶
Adds the object op to the set of container objects tracked by the collector. The collector can run at unexpected times so objects must be valid while being tracked. This should be called once all the fields followed by the tp_traverse handler become valid, usually near the end of the constructor.
msg358731 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2019-12-20 19:20
In summary, a normal allocation for an object tracked by the Garbage collector follows these steps:

1.- Call PyObject_GC_New or PyObject_GC_NewVar or related APIs.
2.- Initialize everything and make sure the object is consistent.
3.- Call PyObject_GC_Track

You can check the implementation of containers types in the stdlib for reference or the documentation for the C API.

Closing this, feel free to reopen of you think there is something missing.
Date User Action Args
2022-04-11 14:58:15adminsetgithub: 68056
2019-12-20 19:20:35pablogsalsetstatus: open -> closed
resolution: not a bug
messages: + msg358731

stage: resolved
2019-12-20 19:11:57pablogsalsetmessages: + msg358729
2019-12-20 16:10:38BTaskayasetnosy: + pablogsal, BTaskaya
type: behavior
2015-04-04 15:00:34h.venevcreate