-- 02: explicit UnloadImage is immediate, idempotent, and does not double-free -- when the object is later collected. local M = dofile("tests/helpers.lua") local R = M.load() M.reset() local b = M.snapshot() local img = R.GenImageColor(4, 4, R.RED) M.ok(img.data ~= nil, "image data allocated") M.eq(M.delta(b, "armed"), 1, "image armed") R.UnloadImage(img) M.eq(M.delta(b, "released"), 1, "explicit unload released exactly once") M.eq(img.data, nil, "img.data nil after unload") M.eq(img.width, 0, "img.width zero after unload") M.eq(img.height, 0, "img.height zero after unload") -- Calling it again is a no-op. R.UnloadImage(img) M.eq(M.delta(b, "released"), 1, "second explicit unload is a no-op") -- Dropping the disarmed object must not release it a second time. img = nil M.gc(3) M.eq(M.delta(b, "released"), 1, "GC after explicit unload is a no-op") -- A plain GC-collected image releases exactly once. local img2 = R.GenImageColor(4, 4, R.RED) img2 = nil M.gc(3) M.eq(M.delta(b, "released"), 2, "GC released the second image exactly once") M.eq(M.delta(b, "errors"), 0, "no finalizer errors") print("ok 02_explicit")