problem with return_stl_iterator policy (luabind 0.7)

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

problem with return_stl_iterator policy (luabind 0.7)

Enno Rehling-2
I have tried to reduce my problem to a very small example. I am trying to
return a std::vector of objects from a function and iterate over them in
lua, but it seems the first element in the vector keeps getting destroyed on
the way to lua. The following code was supposed to output:
42
43
instead it outputs:
148416424
43

-- test.lua:
for k in list() do
    print(k)
end

-- luatest.cpp:
#include "lua.hpp"
#include <luabind/luabind.hpp>
#include <luabind/iterator_policy.hpp>
#include <vector>
#include <iostream>

using namespace luabind;

std::vector<int> Test() {
  std::vector<int> results;
  results.push_back(42);
  results.push_back(43);
  return results;
}

int
main(int argc, char** argv)
{
  lua_State * L = lua_open();
  luaopen_base(L);
  luabind::open(L);

  module(L) [
   def("list", &Test, return_stl_iterator)
  ];

  lua_dofile(L, "test.lua");
  lua_close(L);
  return 0;
}

--
In Paris they simply stared when I spoke to them in French; I never did
succeed in making those idiots understand their language.
- Mark Twain



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: problem with return_stl_iterator policy (luabind 0.7)

Enno Rehling-2
And in case it helps, there's definitely something fishy here, according to
valgrind. I wish I was able to read the templated luabind code, but the
stuff boggles my mind.

ennor@test11 [~/luatest] >>valgrind --tool=memcheck ./luatest
==14353== Invalid read of size 4
==14353==    at 0x804BF11: void
luabind::detail::convert_to_lua<int>(lua_State*, int const&) (in
/home/ennor/luatest/luatest)
==14353==    by 0x804BE78:
luabind::detail::iterator_state<__gnu_cxx::__normal_iterator<int const*,
std::vector<int, std::allocator<int> > > >::step(lua_State*) (in
/home/ennor/luatest/luatest)
==14353==    by 0x804E7A2: luaD_precall (in /home/ennor/luatest/luatest)
==14353==    by 0x804E9DD: luaD_call (in /home/ennor/luatest/luatest)
==14353==  Address 0x1B91E2C0 is 0 bytes inside a block of size 8 free'd
==14353==    at 0x1B90614F: operator delete(void*) (vg_replace_malloc.c:156)
==14353==    by 0x804A740: __gnu_cxx::new_allocator<int>::deallocate(int*,
unsigned) (in /home/ennor/luatest/luatest)
==14353==    by 0x804A5C2: std::_Vector_base<int, std::allocator<int>
>::_M_deallocate(int*, unsigned) (in /home/ennor/luatest/luatest)
==14353==    by 0x804A06D: std::_Vector_base<int, std::allocator<int>
>::~_Vector_base() (in /home/ennor/luatest/luatest)



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: problem with return_stl_iterator policy (luabind 0.7)

Daniel Wallin-2
In reply to this post by Enno Rehling-2
Enno Rehling wrote:
> I have tried to reduce my problem to a very small example. I am trying to
> return a std::vector of objects from a function and iterate over them in
> lua, but it seems the first element in the vector keeps getting destroyed on
> the way to lua. The following code was supposed to output:
> 42
> 43
> instead it outputs:
> 148416424
> 43

The problem is that your std::vector<> is destroyed after the function
call, so the iterators are made invalid. iterator_policy should really
just accept references, so that this would give a compilation error.

Currently, I think the easiest way to accomplish what you want is to
write a thin wrapper over your function that turns the vector into a
table. Something like (untested):

  object Test_wrap(lua_State* L)
  {
      object result = newtable(L);

      int idx = 1;

      std::vector<int> v(Test());

      for (std::vector<int>::iterator i(v.begin()), e(v.end())
        ; i != e; ++i)
      {
          result[idx++] = *i;
      }

      return result;
  }

  module(L) [
    def("list", &Test_wrap, raw(_1))
  ];

--
Daniel Wallin



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user