0

I made a previous post regarding trying to call lua functions from C. You can take a look at that here:

Lua: getting global function failing after loading file

As you can see, I am loading the lua file and then attempting to get the function and call it. One reader suggested my solution to that problem was to change to dofile rather than load file because you need to execute the script to access those functions. But that's not the problem at hand...

Regardless of using dofile or loadfile, When I call the lua_getglobal(L, "abc");
my program crashes...

Callstack:

>   Translation.exe!luaS_newlstr(lua_State * L=0xcccccccc, const char * str=0x00460924, unsigned int l=3)  Line 84 + 0x3 bytes  C
    Translation.exe!lua_getfield(lua_State * L=0xcccccccc, int idx=-10002, const char * k=0x00460924)  Line 551 + 0x20 bytes    C
    Translation.exe!LanguageShovel::FileFound(std::basic_string<char,std::char_traits<char>,std::allocator<char> > path="C:\Loud\Resolution\orchid\source\EAWResolutionApplication.cpp")  Line 32 + 0x16 bytes  C++

Crashes on:

  for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
       o != NULL;
       o = o->gch.next) {

in the lstring.c file in Lua library. I have no idea what the heck is going on in this lua source code. Do you think it could be a lua bug? Or am I just doing this incorrectly?

Running: Windows xp 32 bit.

Community
  • 1
  • 1
Colton Phillips
  • 237
  • 1
  • 4
  • 13
  • 1
    Did you check the `lua[L]_*` calls for errors? I'm sorry to ask such a condescending question but you did not provide us with the code. (And your previous code doesn't do the checks.) – Luc Danton Jul 19 '11 at 22:19
  • 2
    The stack trace shows that your Lua state is 0xcccccccc. How did you create your state? The real question is: what is `lua_open()`? Where did you get this function from? – Omri Barel Jul 19 '11 at 22:41
  • 2
    `lua_open` is a macro defined as `luaL_newstate`. – lhf Jul 19 '11 at 23:39
  • I tested luaLdoFile which returns with no errors. I can't remember exactly what I've done for testing but it's been pretty explicit. If theres something specific you think I should test again I will but as far as I know everything is as expected until the getglobal is called. – Colton Phillips Jul 20 '11 at 00:53

1 Answers1

3

The L=0xcccccccc suggests that you didn't pass the Lua state variable correctly to this function (or that it was lost somewhere between your lua_open and lua_getglobal calls).

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • I'm not sure how that could be... Considering all I do is open the lua libraries, call dofile() which doesn't return an error and then get the global. – Colton Phillips Jul 20 '11 at 00:55
  • 3
    It's hard to help without you showing the code that causes this error. – BMitch Jul 20 '11 at 02:50
  • @Colton: You say `I'm not sure how that could be...`. There is a very quick way to check how that could be: debug your program and watch where and how that first argument to `lua_getglobal` comes out as 0xcccccccc. In VC debug builds that's uninitialized stack variable. – sbk Jul 20 '11 at 13:41
  • Well I've been stepping through my program. In order to understand what's going on you'll need to understand essentially my opensource FileDigger interface http://code.google.com/p/file-digger/source/browse/#svn%2Ftrunk%2FFileDigger if you look at FileDigger.cpp you'll see the workhorse for the recursive operations which my program does. – Colton Phillips Jul 20 '11 at 18:35
  • In my main I create an instance of a FileDigger f and a Shovel s. then I call f.Dig(directory, s). Before any of the recursive operations, s.BeforeDig(path) is called which sets up the lua state. when a file is found in the recursive sequence, it calls s.DirectoryFound(path). I don't know WHY calling a simple method in a class would do this, but once that class is called, it resets the lua state to 0xcccccccccc. If you don't understand what I mean by this description, tell me and I'll try again. I'm hesitant to post source because of proprietary work stuff. but filedigger is open source. – Colton Phillips Jul 20 '11 at 18:43
  • @Colton: There's a good chance you have a memory management bug somewhere (buffer over/under flow, invalid free, etc). Use a memory debugger to find it (http://stackoverflow.com/questions/413477/is-there-a-good-valgrind-substitute-for-windows). You're not supposed to dump a load of code on here anyway, only a minimum example, which would have everything proprietary stripped out. – BMitch Jul 20 '11 at 19:55