Quirrel4.41.0

debug.resurrectunreachable

Defined in module "debug"
from "debug" import resurrectunreachable
resurrectunreachable(): array|null

Resurrects unreachable objects for inspection

examples/debug/resurrectunreachable-basic.nut
from "debug" import resurrectunreachable

// a reference cycle: refcounting cannot free this by itself
local squad = {}
local enemy = {}
squad.rival <- enemy
enemy.rival <- squad
squad = null
enemy = null

let found = resurrectunreachable()
println("found.len() > 0 =", found.len() > 0)
Output:
found.len() > 0 = true

Return value

An array of the unreachable objects it found, or null when there are none.

Notes

This runs the same mark pass as collectgarbage, but where collectgarbage would free what it finds, this function links the unreachable objects back into the VM's live chain and returns them in the array. Holding that array keeps them alive; once nothing still references it, they are unreachable again and a later collectgarbage reclaims them as usual. Use this to inspect what would be collected, such as a leaked reference cycle, without destroying it first.

Example

examples/debug/resurrectunreachable.nut
from "debug" import resurrectunreachable

println("resurrectunreachable() == null =", resurrectunreachable() == null) // nothing pending yet

// a reference cycle: refcounting cannot free this by itself
local a = {}
local b = {}
a.other <- b
b.other <- a
a = null
b = null

let found = resurrectunreachable()
println("type(found) =", type(found))
println("found.len() > 0 =", found.len() > 0)
Output:
resurrectunreachable() == null = true
type(found) = array
found.len() > 0 = true

See also

collectgarbageRuns the garbage collector and returns the number of reclaimed objects
debugmodule index