# 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]

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628344
• Total Posts
2982186

• 9
• 24
• 9
• 9
• 13