3

I'm going for absolute minimalism here. (It's been a while since I've worked with the Lua C API.)

#include <lua.hpp>
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char** argv)
{
    lua_State* state = luaL_newstate();
    luaL_openlibs(state);

    string input;

    while (getline(cin, input))
    {
        auto error = luaL_dostring(state, input.c_str());

        if (error)
        {
            cerr << "Lua Error: " << lua_tostring(state, -1) << '\n';
            lua_pop(state, 1);
        }
    }

    lua_close(state);
    return 0;
}

This program works fine as long as I feed it perfect Lua. However, if I enter something bad (such as asdf()), the program crashes! Why is it not handling my error gracefully?

I've tried breaking out the calls before. It crashes on the call to lua_pcall itself. I never make it past that line.

TheBuzzSaw
  • 8,648
  • 5
  • 39
  • 58
  • Have you checked `stderr`? – imreal Mar 27 '14 at 19:04
  • 2
    Your program works fine for me. (I had to change `auto` to `int`, though.) – lhf Mar 27 '14 at 19:07
  • I have the same results as @lhf. What compiler are you using? What does "crashes" mean here exactly? What operating system is this? – Etan Reisner Mar 27 '14 at 19:09
  • Wellllll.... it seems I was just unlucky. See my answer below. :( – TheBuzzSaw Mar 27 '14 at 19:13
  • @EtanReisner, it was essentially a Windows seg fault. "This program has stopped working." It literally just exits the program the instant it reaches `lua_pcall`. You should be able to reproduce my results if you download the 5.2.1 x64 static library. – TheBuzzSaw Mar 27 '14 at 19:16

1 Answers1

1

The binary download (5.2.1 I believe) has a bug that was corrected in 5.2.3. I rebuilt the library from source, and now my program works fine.

TheBuzzSaw
  • 8,648
  • 5
  • 39
  • 58