Re: smart pointers, slicing, and double deletion

Nigel Atkinson <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <1259214090.2467.9.camel@finwe>
I've found a problem with my code.  The lua ref that is created keeps
the Lua class instance alive, regardless if there is *anything* else
referencing it.

The lua_ref call bumps the smart pointers ref count up by one.

Additional Lua vars pointing at the instance do *not* increase the smart
pointers ref count.

So how to tell that the reference associated with the instance is the
last one, and should be removed? No idea. 

The shared_ptr_converter must have this problem too?

Time for a beer and turn the computer off methinks.

Nigel

On Thu, 2009-11-26 at 02:23 +1300, Nigel Atkinson 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
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.