A problem about extending lua

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

A problem about extending lua

陈果
Hi, everyone. I'm working on implementing a binding between lua and our own component system. Now I'm confronting an odd problem. Following is a fraction of my code:
LUAELALIB_API int luaopen_elalua(lua_State *L) {
    lua_newtable(L);
    return 1;
 }
when I run my program like "require ("myext")", there'll occur a memory corruption.
I'm using lua5.1, but learning lua from pil 1st edition. I want to know whether there is any wrong with my code?
btw, our system has an mechanism to detect memory corruption. I don't know whether there are any tools to detect that on other system.
 
Thank you!


--
Best Regards.

Chen guo
Tongji University, China
Reply | Threaded
Open this post in threaded view
|

Re: A problem about extending lua

Matt Campbell-2
Hello Chen:

What operating system are you running on, and what is the definition of
LUAELALIB_API?  Also I'm guessing you're building your extension as a
shared library or DLL; if so, what are you calling that library?  I'm
not sure what's causing memory corruption, but I'd rewrite the code you
provided as follows:

static const luaL_Reg elalualib[] = {
  /* add any global functions here */
  {NULL, NULL}
};

LUAELALIB_API int luaopen_elalua(lua_State *L) {
  luaL_register(L, "elalua", elalualib);
  /* The elalua table is now on the stack; add any manipulation of that
table here. */
  return 1;
}

On Windows, if you're building your extension as a DLL, LUAELALIB_API
should be defined as __declspec(dllexport).  Also, if you're using a Lua
5.1 DLL (e.g. lua51.dll), you should define LUA_BUILD_AS_DLL when
building your extension.  Finally, on all platforms, make sure you build
your extension with the same luaconf.h that was used to build Lua.

I hope these suggestions help.  Of course, it would also be a good idea
to buy the second edition of PIL so you can learn to make the best use
of Lua 5.1's new features.

--
Matt Campbell
Lead Programmer
Serotek Corporation
www.freedombox.info
"The Accessibility Anywhere People"

Reply | Threaded
Open this post in threaded view
|

Re: A problem about extending lua

陈果
Thank you very much and I'm very appreciated about your suggestion.
 
I've solved the problem. I built the lua library as a static LIB, and built my own extension as a DLL.In the lib, there was a static global variable "dummynode" in ltable.c, so there would be two copy of this var in the lua.exe and my dll, that caused the memory corruption.Now I build the lua lib as a DLL, all things go well!

BTW, it seems hard to buy the 2nd edition of pil in China now and the price of the overseas edition is very high.So what a pity for me.