Re: [luabind] smart pointers, slicing, and double deletion
Brought to you by:
arvidn,
daniel_wallin
From: Daniel W. <da...@bo...> - 2009-11-25 22:34:21
|
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 |