Re: Preventing garbage collection of objects created with Luabind

Nikolas Bowe <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
you need to keep a reference somewhere in lua

you can use the registry
or you can look at wrap_base
or you can look at transferring pointer ownership between Lua and C++ 
(ie you could have push_back adopt the pointer into C++)
you can also try a smart pointer holder like tony suggested. (that way 
lua just destroys the smart pointer instead of the whole object)

we use smart pointers and/or wrappers (wrap_base) for most of our stuff..
we have very few cases of registry stashing and few cases of manual 
ownership transfer.



Greg Santucci wrote:
>
> Hi Luabinders,
>
> I've registered some functions of an stl vector using Luabind the 
> following way without any problems:
>
>     module(g_pLuaState)
>     [
>         class_<std::vector<Component *>>("ComponentSet")
>             .def(constructor<>())
>             .def("push_back",&std::vector<Component *>::push_back)
>             .def("at",(Component*&(std::vector<Component 
> *>::*)(size_t))&std::vector<Component *>::at)
>     ];
>
>     module(g_pLuaState)
>     [
>         class_<Component>("Component")
>             .def(constructor<>())
>             .def("GetPtr", &Component::GetPtr)
>             .def("Speak", &Component::Speak)
>     ];
>
>     module(g_pLuaState)
>     [
>         class_<Lamp, bases<Component>>("Lamp")
>             .def(constructor<>())
>     ];
>
>     module(g_pLuaState)
>     [
>         class_<Relay, bases<Component>>("Relay")
>             .def(constructor<>())
>     ];
>
> At the Lua repl, I can instance this vector, and add elements to it:
>
> > c = ComponentSet()
> > c:push_back(Relay():GetPtr())
> > c:push_back(Lamp():GetPtr())
>
> And I can access the contents of the vector:
>
> > print(c:at(0):Speak())
> Relay
> > print(c:at(1):Speak())
> Lamp
>
> However, eventually, the Relay and Lamp objects I created earlier will 
> be garbage collected, since they aren't bound to anything that Lua can 
> see, so if I continue accessing the vector in this way, after about 14 
> or so accesses, I get the following error message:
>
> > print(c:at(0):Speak())
> std::exception: 'Access violation - no RTTI data!'
>
>
> Naturally, if I do this instead:
>
> > c = ComponentSet()
> > r1 = Relay()
> > l1 = Lamp()
> > c:push_back(r1:GetPtr())
> > c:push_back(l1:GetPtr())
>
> This problem doesn't occur.
>
> My question is, is there a way to prevent these vector elements from 
> being garbage collected without having to keep another list of them in 
> lua?
>
> Also, I had to define a GetPtr() function for the Component class 
> which looks like this:
>
> Component * Component::GetPtr() { return this; }
>
> Is there a way I can get to the pointer from Lua without having to 
> create this function?
>
> Just want to finish up by saying Lua and Luabind are awesome, and I am 
> grateful to the authors and maintainers of these tools.
>
> Regards,
> Greg Santucci
>
>
> ------------------------------------------------------------------------
>
> ------------------------------------------------------------------------------
> Download Intel&#174; Parallel Studio Eval
> Try the new software tools for yourself. Speed compiling, find bugs
> proactively, and fine-tune applications for parallel performance.
> See why Intel Parallel Studio got high marks during beta.
> http://p.sf.net/sfu/intel-sw-dev
> ------------------------------------------------------------------------
>
> _______________________________________________
> luabind-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>   




This message and its attachments may contain legally privileged or confidential information. This message is intended for the use of the individual or entity to which it is addressed. If you are not the addressee indicated in this message, or the employee or agent responsible for delivering the message to the intended recipient, you may not copy or deliver this message or its attachments to anyone. Rather, you should permanently delete this message and its attachments and kindly notify the sender by reply e-mail. Any content of this message and its attachments, which does not relate to the official business of the sending company must be taken not to have been sent or endorsed by the sending company or any of its related entities. No warranty is made that the e-mail or attachment(s) are fr
 ee from computer virus or other defect.

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev

_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.