added two functions for safely managing resource external to Lua