lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great John Dunn once stated:
> >  If you are using Linux, could you run the app under valgrind?  It will
> >  take longer, but it should reveal some issues.
> 
> Running on Ubuntu using g++ 9.3.0 it is still crashing. Here is the output
> of valgrind. It definitely detected an error - the issue for me is that's
> the error is deep inside of Lua and I'm not sure how my code could have
> caused this. This is using Lua 5.3 from apt-get so it doesn't contain any
> modifications.

  I went back to the original example you provided, recompiled it, and yes,
I finally got a segfault.  Using valgrind, I was able to find the problem in
this function:

	struct mytimer
	{
	  lua_State* L;
	  int handler_index{ LUA_REFNIL };
	  void trigger()
	  {
	    std::cout << __FUNCTION__ << std::endl;
	    lua_rawgeti(L, LUA_REGISTRYINDEX, handler_index);
	    lua_pcall(L, 0, 0, 0);
	  }
	};

  My C++ isn't that strong, but the use of L here, while it compiles,
doesn't seem to work.  I changed the code to read:

	struct mytimer
	{
	  lua_State* L;
	  int handler_index{ LUA_REFNIL };
	  void trigger(lua_State *Ls)
	  {
	    std::cout << __FUNCTION__ << std::endl;
	    lua_rawgeti(Ls, LUA_REGISTRYINDEX, handler_index);
	    lua_pcall(Ls, 0, 0, 0);
	  }
	};

and fix the one call to trigger() to include the Lua state variable, and
that fixed the segfault right up.  And when I added lua_close(L); to the end
of the run() function, all errors from valgrind cleared up (except the
memory leak from "*timer = new mytimer();" call).  I checked this against
both stock Lua 5.3.0 and Lua 5.3.2 and both are now running clean with my
changes.

  -spc