Returning multiple objects "by value"

Glen Fraser <[email protected]> Wed, 7 May 2014 17:47:18 +0200
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
--===============8310028980827093014==
Content-Type: multipart/alternative;
	boundary="Apple-Mail=_19926CD2-A0A0-47DC-9D6B-7BC1AE1FA050"


--Apple-Mail=_19926CD2-A0A0-47DC-9D6B-7BC1AE1FA050
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=us-ascii

Hi there, I'm trying to see if there is anything wrong with returning a =
table of values, where the values are C++ objects (created on the stack =
-- do they get copied so that Lua "owns" them?).  If they were really =
referenced on the stack, I'd expect them to be invalid, or for the =
program to crash, when I use the returned values much later in Lua code. =
 But all seems to work, with no crashes, no obvious memory leaks after =
running for many iterations, and the returned objects contain the =
expected data.

Is this "okay" to do?  (contrived example, but essentially doing the =
same as my real code)

luabind::object returnObjects()
{
    luabind::object retTable =3D luabind::newtable(getLuaInstance());
    for (size_t i =3D 0; i < 10; ++i) {
        retTable[i+1] =3D SomeClass("some initialization data", i);
    }
    return retTable;
}

returnObjects is registered like this:

     def("returnObjects", &returnObjects),
     // ...


SomeClass is registered like this:

     class_<SomeClass>("SomeClass")
     .def(constructor<>())
     .def(constructor<string, int>())
     // ...

I know I could register SomeClass to be held in a smart pointer, then =
have returnObjects return an STL vector of them (using the =
return_stl_iterator policy), but I'd like to understand what is going on =
with the above code, because as I say, it seems to work fine.

Is there any problem with returning non-primitive objects this way (in a =
table)?  Is each object copied, so Lua has its own instance?

Instead of a Lua table, first I tried the same returning a vector of =
SomeClass (not of SomeClass pointers), and using the return_stl_iterator =
policy, but the objects must have been freed back by the time I was back =
in Lua-land(?), because iterating over the returned vector in Lua showed =
the instances contained garbage values.

Is what I'm doing above correct, or is there a better way?

Thanks,
Glen.


--Apple-Mail=_19926CD2-A0A0-47DC-9D6B-7BC1AE1FA050
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=us-ascii

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html =
charset=3Dus-ascii"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi =
there, I'm trying to see if there is anything wrong with returning a =
table of values, where the values are C++ objects (created on the stack =
-- do they get copied so that Lua "owns" them?). &nbsp;If they were =
really referenced on the stack, I'd expect them to be invalid, or for =
the program to crash, when I use the returned values much later in Lua =
code. &nbsp;But all seems to work, with no crashes, no obvious memory =
leaks after running for many iterations, and the returned objects =
contain the expected data.<div><br></div><div>Is this "okay" to do? =
&nbsp;(contrived example, but essentially doing the same as my real =
code)<div><div><br></div><div><div style=3D"margin: 0px; font-size: =
16px; font-family: Inconsolata; ">luabind::object =
returnObjects()</div><div style=3D"margin: 0px; font-size: 16px; =
font-family: Inconsolata; ">{</div><div style=3D"margin: 0px; font-size: =
16px; font-family: Inconsolata; ">&nbsp; &nbsp;&nbsp;luabind::object =
retTable =3D luabind::newtable(getLuaInstance());</div><div =
style=3D"margin: 0px; font-size: 16px; font-family: Inconsolata; =
">&nbsp; &nbsp; <span style=3D"color: #bb2ca2">for</span> (size_t i =3D =
<span style=3D"color: #272ad8">0</span>; i &lt; 10; ++i) {</div><div =
style=3D"margin: 0px; font-size: 16px; font-family: Inconsolata; =
">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;retTable[i+<span style=3D"color: =
rgb(39, 42, 216); ">1</span>] =3D SomeClass("some initialization data", =
i);</div><div style=3D"margin: 0px; font-size: 16px; font-family: =
Inconsolata; color: rgb(0, 132, 0); ">&nbsp; &nbsp;&nbsp;}</div><div =
style=3D"margin: 0px; font-size: 16px; font-family: Inconsolata; =
">&nbsp; &nbsp; <span style=3D"color: #bb2ca2">return</span> =
retTable;</div><div style=3D"margin: 0px; font-size: 16px; font-family: =
Inconsolata; ">}</div></div><div><br></div><div>returnObjects is =
registered like this:</div><div><br></div><div><div style=3D"margin: =
0px; font-size: 16px; font-family: Inconsolata; color: rgb(49, 89, 93); =
"><span style=3D"color: #000000">&nbsp; &nbsp; &nbsp;</span>def<span =
style=3D"color: #000000">(</span><span style=3D"color: =
#d12f1b">"returnObjects"</span><span style=3D"color: #000000">, =
&amp;</span>returnObjects<span style=3D"color: =
#000000">),</span></div></div><div><div style=3D"margin: 0px; font-size: =
16px; font-family: Inconsolata; "><div style=3D"margin: 0px; color: =
rgb(0, 132, 0); ">&nbsp; &nbsp; &nbsp;// =
...</div><div><br></div></div></div><div><br></div><div>SomeClass is =
registered like this:</div></div><div><br></div><div><div style=3D"margin:=
 0px; font-size: 16px; font-family: Inconsolata; color: rgb(49, 89, 93); =
"><span style=3D"color: #000000">&nbsp; &nbsp; &nbsp;</span>class_<span =
style=3D"color: #000000">&lt;</span>SomeClass<span style=3D"color: =
#000000">&gt;(</span><span style=3D"color: =
#d12f1b">"SomeClass"</span><span style=3D"color: =
#000000">)</span></div><div style=3D"margin: 0px; font-size: 16px; =
font-family: Inconsolata; color: rgb(49, 89, 93); "><span style=3D"color: =
#000000">&nbsp;&nbsp; &nbsp; .</span>def<span style=3D"color: =
#000000">(</span>constructor<span style=3D"color: =
#000000">&lt;&gt;())</span></div><div style=3D"margin: 0px; font-size: =
16px; font-family: Inconsolata; ">&nbsp;&nbsp; &nbsp; .<span =
style=3D"color: #31595d">def</span>(<span style=3D"color: =
#31595d">constructor</span>&lt;<span style=3D"color: =
#bb2ca2">string</span>, <span style=3D"color: =
#bb2ca2">int</span>&gt;())</div></div><div style=3D"margin: 0px; =
font-size: 16px; font-family: Inconsolata; "><div style=3D"margin: 0px; =
color: rgb(0, 132, 0); ">&nbsp; &nbsp;&nbsp;&nbsp;// =
...</div></div><div><br></div><div><div>I know I could register =
SomeClass to be held in a smart pointer, then have returnObjects return =
an STL vector of them (using the return_stl_iterator policy), but I'd =
like to understand what is going on with the above code, because as I =
say, it <i>seems</i> to work fine.</div></div><div><br></div><div>Is =
there any problem with returning non-primitive objects this way (in a =
table)? &nbsp;Is each object copied, so Lua has its own =
instance?</div><div><br></div><div>Instead of a Lua table, first I tried =
the same returning a vector of SomeClass (not of SomeClass pointers), =
and using the return_stl_iterator policy, but the objects must have been =
freed back by the time I was back in Lua-land(?), because iterating over =
the returned vector in Lua showed the instances contained garbage =
values.</div><div><br></div><div>Is what I'm doing above correct, or is =
there a better =
way?</div><div><br></div><div>Thanks,</div><div>Glen.</div><div><br></div>=
</div></body></html>=

--Apple-Mail=_19926CD2-A0A0-47DC-9D6B-7BC1AE1FA050--


--===============8310028980827093014==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

------------------------------------------------------------------------------
Is your legacy SCM system holding you back? Join Perforce May 7 to find out:
&#149; 3 signs your SCM is hindering your productivity
&#149; Requirements for releasing software faster
&#149; Expert tips and advice for migrating your SCM now
http://p.sf.net/sfu/perforce
--===============8310028980827093014==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user

--===============8310028980827093014==--