• Access violation when exporting a C++ class to Lua using LuaBind
    8 replies, posted
I'm trying to export a simple class to Lua using LuaBind. I took the code from two sites which showed roughly the same way to do it, but it's still failing. [lua]// Default headers #include <iostream> #include <string> // Lua headers extern "C" { #include "lua.h" #include "lualib.h" #include "lauxlib.h" } #include "luabind/luabind.hpp" // Sample class class NumberPrinter { public: NumberPrinter( int number ) : m_number( number ) {} void print() { std::cout << m_number << "\n"; } private: int m_number; }; int main() { // Create Lua state and load sample file lua_State *luaState = lua_open(); luabind::open( luaState ); // Set up bind to number class luabind::module( luaState ) [ luabind::class_<NumberPrinter>( "NumberPrinter" ) .def( luabind::constructor<int>() ) .def( "print", &NumberPrinter::print ) ]; // Use the class in Lua luaL_dostring( luaState, "Print2000 = NumberPrinter(2000)\n" "Print2000:print()\n" ); // Clean up Lua state lua_close( luaState ); getchar(); return 0; }[/lua] When running that code, luabind::module causes the following runtime error and has no other information in debug mode: [code]Unhandled exception at 0x690008f5 in Lua Playground.exe: 0xC0000005: Access violation.[/code] Any ideas? I tried [url=http://www.rasterbar.com/products/luabind/docs.html#binding-classes-to-lua]the official website[/url] and [url=http://www.nuclex.org/articles/cxx/1-quick-introduction-to-luabind]this introduction[/url].
The only thing I could think of.. are you using the __cdecl calling convention?
Use luaL_newstate instead of lua_open. I'm pretty sure states created with lua_open don't have the default memory allocator set up.
[QUOTE=jA_cOp;19171478]Use luaL_newstate instead of lua_open. I'm pretty sure states created with lua_open don't have the default memory allocator set up.[/QUOTE] Nope, same thing.
Step through the code in a debugger and see which line causes the access violation exception.
It causes that error on the luabind::module line.
Then I've no idea. Did you try any of the official example code, unedited, with the same linker settings? If that gives you an access violation too, then you might have the wrong version of luabind or something. [QUOTE=ZeekyHBomb;19171391]The only thing I could think of.. are you using the __cdecl calling convention?[/QUOTE] And remember to check this too, if you haven't already (since you didn't reply to zeeky).
Are you linking to the same runtime for Luabind and your project?
Turned out I had to disable "Buffer Security Check" and enable string pooling in C++ -> Code Generation in VS. [url=http://pastie.org/755691]Here[/url]'s the final code if anyone's interested. Nonetheless, thanks for the help guys.
Sorry, you need to Log In to post a reply to this thread.