Re: smart pointers, slicing, and double deletion

Nigel Atkinson <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <1259171621.2477.34.camel@finwe>
Hmmmm, I must run my test program through valgrind.  And apply that
patch you sent... anyhow,

I think the problem I was hitting is that the smart pointer that the
converter in shared_ptr_convertor.hpp was different to the one that you
got a copy of if you called a C++ function from Lua that took a smart
pointer as a parameter.

I'm not sure which one is a copy of the smart pointer that is actually
holding the instance in the internals of luabind, but I suspect its the
one passed to functions, since besides the slicing, my program was
showing expected ref counts in the pointers.  Perhaps not.

Nigel

On Wed, 2009-11-25 at 10:25 -0500, Evan Wies wrote:
> I am seeing some similar problems.  Valgrind is not happy when my objects are destroyed -- I think
> the lua_State is getting mucked with after it has closed.  In your case, it is probably the thread's
> state that is being altered post-mortem.
> 
> One shouldn't have to jump through hoops to do this.  I'm exploring and will let you know if I come
> up with anything.
> 
> -Evan
> 
> 
> 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
> _______________________________________________
> 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.