简体   繁体   English

使用LuaBind将C ++类导出到Lua时发生访问冲突

[英]Access violation when exporting a C++ class to Lua using LuaBind

I'm trying to export a simple class to Lua using LuaBind. 我正在尝试使用LuaBind将简单的类导出到Lua。 I took the code from two sites which showed roughly the same way to do it, but it's still failing. 我从两个站点获取了代码,这些代码显示出大致相同的方式,但是仍然失败。

// 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;
}

When running that code, luabind::module causes the following runtime error and has no other information in debug mode: 运行该代码时,luabind :: module会导致以下运行时错误,并且在调试模式下没有其他信息:

Unhandled exception at 0x690008f5 in Lua Playground.exe: 0xC0000005: Access violation. Lua Playground.exe中0x690008f5处未处理的异常:0xC0000005:访问冲突。

I would encourage you to get this started with the binaries and sample VS2008 solution available from this website . 我鼓励您从此网站上提供的二进制文件和VS2008示例解决方案开始。 It has the exact same sample code you are trying to run (minus the typos) and it worked well on my machine. 它具有与您尝试运行的示例代码完全相同的示例代码(减去输入错误),并且在我的机器上运行良好。 If it still doesn't work, you'll need help from the Lua community. 如果仍然无法使用,则需要Lua社区的帮助。 A minidump is probably required to help them diagnose this, just the exception message isn't enough. 可能需要一个小型转储来帮助他们进行诊断,仅异常消息是不够的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM