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.

4
  • 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.) Commented Jul 19, 2011 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? Commented Jul 19, 2011 at 22:41
  • 2
    lua_open is a macro defined as luaL_newstate. Commented Jul 19, 2011 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. Commented Jul 20, 2011 at 0:53

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

6 Comments

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.
It's hard to help without you showing the code that causes this error.
@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.
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 code.google.com/p/file-digger/source/browse/… if you look at FileDigger.cpp you'll see the workhorse for the recursive operations which my program does.
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.