Re: smart pointers, slicing, and double deletion
Nigel Atkinson <[email protected]>
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <1259170629.2477.19.camel@finwe> |
Yes that would be one way to go, for less grief. However I'm creating more specialised widgets in Lua. The C++ side of things has no knowledge of them except that they are a 'type' of widget. I guess its not too un-maintainable. The hard part was figuring out what and how to do it. As long as there are comments explaining the whys and how, someone else or me in a few months should be able to update it. The biggest danger in the code is the use of detail::wrap_access::ref which may change in a future release of luabind. Nigel On Wed, 2009-11-25 at 11:44 -0500, Taesoo Kwon wrote: > I also would like your approach if it is possible and easy to > maintain. > But how about making C++ side control all the memory > allocation/deallocation? > > For example, in lua part, only widget names are stored, and every time > a pointer is needed, you use an interface function to check and > retrieve if such widget exists. > > Taesoo > > On Wed, Nov 25, 2009 at 8:23 AM, Nigel Atkinson > <[email protected]> wrote: > Smart pointers, slicing, and double deletion. > > Sound's like a right mess doesn't it. I finaly cracked it, > and since > I'm sure someone else will come across the same problem I had, > I thought > I'd share. > > What I was trying to do: > I have a GUI widget (Ogre3d if your wondering), that holds a > list of its > children widgets. This widget class is the base for several > Lua widget > classes. I'm using smart pointers to avoid adoption issues. > > Originally using shared pointers, I found that when only C++ > had a > pointer to the instance, it would get 'sliced' if it was a Lua > class. > > Bring in shared_ptr_converter.hpp, and that fixes the > slicing. However > because of part of the way it works, it causes double deletion > albeit > seemingly only at the shut down of a Lua thread. (Its not safe > across > Lua threads either, but that's easily fixed.) > > So to fix the double counting, I started thinking about making > my own > shared pointer - oh man, do I have to? - then realised that > boost::intrusive_ptr would do most of the job. Also rather > than store > the counts in the widget class, I just put them in to a map. > Handy for > debugging. I still might move that data to the widget class, > however > this would work if you could not change the class. > > So that's the ref counting working now, on to the slicing > problem. I > added code to my intrusive_ptr_add_ref and > intrusive_ptr_release > functions, to create and remove, a Lua ref i.e. lua_ref(), > lua_unref() > at the appropriate time. The trick was getting the pointer to > the class > representation on to the Lua stack. After a bit of following > the code > line by line as my test program made class instances, I found > the bit of > luabind code I needed. > > Attached is my test program, and a Lua script to drive it. > I'd be > interested in any comments any of you have about it! > > Nigel > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal > Reports 2008 30-Day > trial. Simplify your report design, integration and deployment > - and focus on > what you do best, core application coding. Discover what's new > with > Crystal Reports now. http://p.sf.net/sfu/bobj-july > _______________________________________________ > luabind-user mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/luabind-user > > > ------------------------------------------------------------------------------ > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day > trial. Simplify your report design, integration and deployment - and focus on > what you do best, core application coding. Discover what's new with > Crystal Reports now. http://p.sf.net/sfu/bobj-july > _______________________________________________ luabind-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/luabind-user ------------------------------------------------------------------------------ Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july