luabind を使用して stl::vector::iterator を lua スクリプトに返すときに奇妙な問題が発生しました。
以下はコードです:
1) lua スクリプトによって呼び出される 2 つの関数を作成しました。
std::vector<car*> get_car_list()
{
std::vector<car*>* vec = new std::vector<car*>();
vec->push_back(new car("I'm the 1st"));
vec->push_back(new car("I'm the 2nd"));
return *vec;
}
void output(const std::string& msg)
{
std::cout << "lua:" << msg << std::endl;
}
2) 関数を lua にバインドします
luabind::module(L)
[
luabind::def("get_car_list", &get_car_list, luabind::return_stl_iterator)
];
luabind::module(L)
[
luabind::def("output", &output)
];
3) 以下のようなスクリプトを実行します。
function test()
items = get_car_list();
for item in items do
output(item:get_name());
end
end
4)結果は次のとおりです。出力ウィンドウには、次のように表示されます。
lua:I'm the 1st
そして、プログラムは luabind/policy.hpp:754 で壊れています
template <>
struct default_converter<std::string>
: native_converter_base<std::string>
{
.....
void to(lua_State* L, std::string const& value)
{
lua_pushlstring(L, value.data(), value.size()); // !!Break Here with Error EXC_BAD_ACCESS
}
};
std::vector のすべての要素を表示したいのですが、最初の要素しか表示されずにクラッシュします。
どうもありがとうございます!:)
ジェイソン