swig/Examples/test-suite/lua/exception_memory_leak_runme.lua
Olly Betts 9ab9c71623 [lua] Run destructors of local C++ objects on SWIG_fail
Arrange that destructors of local C++ objects in the wrapper function
get run on SWIG_fail (which calls lua_error() which calls longjmp()).

We achieve this by putting almost everything in the function in its
own block, and end that right before lua_error() at which point those
destructors will get called.
2022-10-14 14:44:19 +13:00

29 lines
960 B
Lua

require("import") -- the import fn
import("exception_memory_leak") -- import code
eml=exception_memory_leak --alias
-- catch "undefined" global variables
local env = _ENV -- Lua 5.2
if not env then env = getfenv () end -- Lua 5.1
setmetatable(env, {__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
a = eml.Foo()
assert(eml.Foo_get_count() == 1)
b = eml.Foo()
assert(eml.Foo_get_count() == 2)
-- Normal behaviour
eml.trigger_internal_swig_exception("no problem", a)
assert(eml.Foo_get_count() == 2)
assert(eml.Foo_get_freearg_count() == 1)
-- SWIG exception triggered and handled (return new object case)
ok,ex=pcall(eml.trigger_internal_swig_exception, "null", b)
assert(ok==false)
assert(eml.Foo_get_count() == 2)
assert(eml.Foo_get_freearg_count() == 2)
-- SWIG exception triggered and handled (return by value case).
ok,ex=pcall(eml.trigger_internal_swig_exception, "null")
assert(ok==false)
assert(eml.Foo_get_count() == 2)