Catching 'out of memory' using pcall()

  • From: Dmitri Shubin <sbn@xxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 13 Mar 2014 11:06:24 +0400

Hi list, Mike!

Is it possible to catch 'out of memory' condition using pcall() ?

The test I tried shows a bit contradictory results:

$ cat -n a.lua
     1  local function foo()
     2      local t = {}
     3      local i = 0
     4      while true do
     5          t[i] = tostring(i)
     6          i = i + 1
     7      end
     8  end
     9
    10  local r = { pcall(foo) }
    11  print(r[1], r[2])
$ ./src/luajit -v
LuaJIT 2.0.3 -- Copyright (C) 2005-2014 Mike Pall. http://luajit.org/
$ ./src/luajit a.lua
PANIC: unprotected error in call to Lua API (not enough memory)

But if I change line 5 to
     5          t[i] = "x" .. tostring(i)

I get

$ ./src/luajit a.lua
false   not enough memory

I'm on Linux, x86_64

Thanks!

Other related posts: