Re: smart pointers, slicing, and double deletion

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

How so? Does the test case you attached show this if I replace
intrusive_ptr with shared_ptr?

> (Its not safe across Lua threads either, but that's easily fixed.)

Because it stores a handle with the thread lua_State*, which might later
be collected? Yes, nice catch. This should fix it:

diff --git a/luabind/shared_ptr_converter.hpp
b/luabind/shared_ptr_converter.hpp
index d35d805..afdc841 100644
--- a/luabind/shared_ptr_converter.hpp
+++ b/luabind/shared_ptr_converter.hpp
@@ -5,6 +5,7 @@
 #ifndef LUABIND_SHARED_PTR_CONVERTER_090211_HPP
 # define LUABIND_SHARED_PTR_CONVERTER_090211_HPP

+# include <luabind/get_main_thread.hpp>
 # include <luabind/handle.hpp>
 # include <luabind/detail/policy.hpp>
 # include <boost/shared_ptr.hpp>
@@ -17,7 +18,7 @@ namespace detail
   struct shared_ptr_deleter
   {
       shared_ptr_deleter(lua_State* L, int index)
-        : life_support(L, index)
+        : life_support(get_main_thread(L), index)
       {}

       void operator()(void const*)

-- 
Daniel Wallin
BoostPro Computing
http://www.boostpro.com

------------------------------------------------------------------------------
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.