-- 09: every owned-pointer return releases exactly one allocation at GC time. local M = dofile("tests/helpers.lua") local ffi = require("ffi") local R = M.load() M.reset() local img = R.GenImageColor(2, 2, R.RED) M.ok(img.data ~= nil, "source image valid") -- Baseline after the image exists: the image is armed too, and it is released -- explicitly at the end of the case. local b = M.snapshot() local count = M.out_int() local comp_size = M.out_int() -- Keep every result alive until the batch is complete, then drop them all at -- once so a single GC verifies the whole set. local keep = {} keep[#keep + 1] = R.LoadImageColors(img) keep[#keep + 1] = R.LoadImagePalette(img, 8, count) keep[#keep + 1] = R.LoadRandomSequence(4, 1, 5) keep[#keep + 1] = R.LoadCodepoints("abc", count) keep[#keep + 1] = R.LoadTextLines("a\nb\n", count) keep[#keep + 1] = R.CompressData("abcd", 4, comp_size) keep[#keep + 1] = R.DecodeDataBase64( R.EncodeDataBase64("abcd", 4, count), count) keep[#keep + 1] = R.TextReplaceAlloc("a", "a", "b") local n = #keep M.ok(n >= 8, "created at least 8 owned pointers, got " .. n) for i = 1, n do M.ok(keep[i] ~= nil, "pointer " .. i .. " is non-nil") end keep = nil M.gc(3) -- 7 of the 8 results are owned pointers; EncodeDataBase64/TextReplaceAlloc -- return owned strings that the wrapper copies and frees immediately (valgrind -- proves those). Every owned pointer must be armed and released exactly once. M.ok(M.delta(b, "armed") >= 7, string.format("expected >=7 armed pointers, got %d", M.delta(b, "armed"))) M.ok(M.delta(b, "released") >= 7, string.format("expected >=7 released pointers, got %d", M.delta(b, "released"))) M.eq(M.delta(b, "errors"), 0, "no finalizer errors") R.UnloadImage(img) print("ok 09_pointers")