Re: smart pointers, slicing, and double deletion

Taesoo Kwon <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
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
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.