Sign in to follow this  
beun

Luabind & return_stl_iterator

Recommended Posts

Hello, I've been having some problems with Luabind and return_stl_iterator. Here's a sample:
std::vector<int> TestVector()
{
	std::vector<int> vec;
	vec.push_back(1);
	vec.push_back(5);
	vec.push_back(9);
	vec.push_back(7);
	return vec;
}

void Script::RegisterTypes() // Called when constructing the Script class, which holds lua_State* etc.
{
    using namespace luabind;
    module(m_state)
    [
        def("TestVector", &TestVector, return_stl_iterator)
    ];
}

test.lua:
testIt = TestVector()

for item in testIt do
	print(item)
end

When I run this normally, the program crashes (both the Release and the Debug version). However, when I run the program from MSVC (2008) with F5 (Start debugging) or Ctrl+F5 (Start without debugging), the program runs and exits normally, but the breakpoint in TestVector is never triggered, so Lua never calls that function. (Again, both in the Release and the Debug version.) EDIT: I forgot that MSVC changes the current directory, so the script file was never executed.[ignore] Lua doesn't give an error, and Luabind doesn't throw an exception. I don't know what's causing the problem. I could always use a wrapper function for Lua which puts the vector in a table, but then Luabind would be useless anyway. Does someone know what's causing this? EDIT 2: Apparently return_stl_iterator only works with references (although returning values doesn't give a compile error): Luabind only stores the iterator, not the vector itself. So I wanted to allocate a new vector with new, and use the adopt(result) policy, but the adopt policy only accepts pointers, so I can't use the result_stl_iterator and the adopt policy together. Maybe someone can provide a fix, because now there's apparently no way to pass a vector to Lua without a wrapper function? [Edited by - beun on September 10, 2009 12:53:40 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this