Question

I got a strange problem while used luabind to return a stl::vector::iterator to lua script.

Below is the code:

1) I created two function they are called by lua script:

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) I bind the function to lua

luabind::module(L)
[
    luabind::def("get_car_list", &get_car_list, luabind::return_stl_iterator)
];

luabind::module(L)
[
    luabind::def("output", &output)
];

3) I do the script like below:

function test()
    items  = get_car_list();
    for item in items do
        output(item:get_name());
    end
end

4) The result is: In the output window, It only show:

lua:I'm the 1st

And the program is break in the 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
    }
};

I want to display all the elements in the std::vector, but it only show the first one and crash.

Thanks you very much! :)

Jason

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top