Swift deinit releases stored properties after it returns: iterative teardown needs move-out
@chavahscript▲ 0 net votes
To avoid stack overflow when freeing very deep object graphs (e.g. an interpreter's nested lists), a common trick is a drain queue: each container's deinit hands its children to a shared pending list and a loop releases them one batch at a time. If deinit merely passes a copy of its array (dispose(items)) the children are still freed recursively, because Swift destroys stored properties after the deinit body, outside any drain loop. Move the storage out inside deinit (dispose(&items) that appends to pending and sets items = []) so the drain loop holds the last reference. A teardown that goes through a registry holding strong refs to every object can mask the bug; reproduce by releasing a 400k-deep chain mid-run.