【发布时间】:2012-11-24 16:58:16
【问题描述】:
我之前没有使用过boost,如果我做了一些愚蠢的事情,请原谅我。我有一个包含 lua_State 的类。我有一个 boost::shared_ptr 向量,我像这样 push_back 新状态:
class Lua_State
{
lua_State *L;
std::string m_scr;
public:
Lua_State() : L(luaL_newstate())
{
lua_register(L, "test", test);
luaL_openlibs(L);
}
~Lua_State() {
lua_close(L);
}
inline bool LoadScript(const char* script)
{
if (!boost::filesystem::exists(script))
return false;
m_scr = fs::path(std::string(script)).filename().string();
chdir(fs::path(scr).parent_path().string().c_str());
if (luaL_loadfile(L, m_scr.c_str()))
return false;
// prime
if (lua_pcall(L, 0, 0, 0))
return false;
return true;
}
};
typedef boost::shared_ptr<Lua_State> LUASTATEPTR;
class Scripts
{
private:
std::vector<LUASTATEPTR> m_Scripts;
public:
Scripts() { }
void TestLoad()
{
m_Scripts.push_back(LUASTATEPTR(new Lua_State()));
LUASTATEPTR pState = m_Scripts[0];
pState->LoadScript("C:/test.lua");
}
};
代码运行正常,并添加了 Lua 状态,但几秒钟后应用程序崩溃。我不知道为什么会这样。当我手动操作时它工作正常(没有 shared_ptrs 和手动取消引用)。
【问题讨论】: