Segfault when accessing returned shared_ptr

Stefan Schindler <[email protected]> Fri, 24 Jun 2011 04:14:34 +0200
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
Hi guys,

I'm relatively new to Luabind and have a problem with using a shared_ptr
created in C++ in Lua.

This is the C++ code reduced to a minimum:

        struct Member {
        	Member( int x ) : m_x( x ) {}
        	int GetNum() { return m_x; }
        	int m_x;
        };
        
        template <class T>
        struct Foobar {
        	typedef boost::shared_ptr<Foobar<T> > Ptr;
        
        	Foobar( const T& value ) : x( value ) {}
        	static Ptr Create( const T& value ) { return
        boost::shared_ptr<Foobar<T> >( new Foobar<T>( value ) ); }
        
        	T& Get() { return x; }
        	const T& Get() const { return x; }
        
        	T x;
        };

The binding looks like follows:

        class_<Member>( "Member" )
        	.def( constructor<int>() )
        	.def( "GetNum", &Member::GetNum )
        ,
        class_<Foobar<Member*> >( "Foobar" )
        	.def( "Get",
        (Member*&(Foobar<Member*>::*)())&Foobar<Member*>::Get )
        	.scope [
        		def( "Create", (Foobar<Member*>::Ptr(*)( Member*
        ))&Foobar<Member*>::Create )
        	]

And the Lua code like that:

        member = debug.Member( 123 )
        property = debug.Foobar.Create( member )
        get = property:Get()
        print( get:GetNum() )

As soon as get:GetNum() is called, the program crashes. This is the
backtrace:

        #0  0x00000000004ef1f6 in Member::GetNum (this=0x7b)
        at /home/stefan/code/games/cell/engine/src/cell/LuaBindExport.cpp:22
        #1  0x0000000000511da0 in luabind::detail::invoke_member<int
        (Member::*)(), boost::mpl::vector2<int, Member&>,
        luabind::detail::null_type> (L=0x9e8b60, self=..., ctx=...,
        f=@0x9f5a90)
            at /usr/include/luabind/detail/call.hpp:283
        #2  0x0000000000507722 in luabind::detail::invoke0<int
        (Member::*)(), boost::mpl::vector2<int, Member&>,
        luabind::detail::null_type, boost::is_void<int> > (L=0x9e8b60,
        self=..., ctx=..., f=@0x9f5a90, 
            policies=...) at /usr/include/luabind/detail/call.hpp:78
        #3  0x00000000005064ab in luabind::detail::invoke<int
        (Member::*)(), boost::mpl::vector2<int, Member&>,
        luabind::detail::null_type> (L=0x9e8b60, self=..., ctx=...,
        f=@0x9f5a90, policies=...)
            at /usr/include/luabind/detail/call.hpp:101
        #4  0x0000000000505158 in
        luabind::detail::function_object_impl<int (Member::*)(),
        boost::mpl::vector2<int, Member&>,
        luabind::detail::null_type>::entry_point (L=0x9e8b60)
            at /usr/include/luabind/make_function.hpp:63
        #5  0x00007ffff7bc0a56 in ?? () from /usr/lib/liblua5.1.so.0
        #6  0x00007ffff7bcb811 in ?? () from /usr/lib/liblua5.1.so.0
        #7  0x00007ffff7bc0fc5 in ?? () from /usr/lib/liblua5.1.so.0
        #8  0x00007ffff7bc0647 in ?? () from /usr/lib/liblua5.1.so.0
        #9  0x00007ffff7bc06c5 in ?? () from /usr/lib/liblua5.1.so.0
        #10 0x00007ffff7bbc114 in lua_pcall ()
        from /usr/lib/liblua5.1.so.0
        #11 0x00000000004e3d23 in
        luabind::detail::proxy_function_void_caller<boost::tuples::tuple<boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> >::~proxy_function_void_caller (this=0x7fffffff9830, 
            __in_chrg=<value optimized out>)
        at /usr/include/luabind/detail/call_function.hpp:262


So basically when returning a newly created boost::shared_ptr, there
seems to go something wrong. After hours of debugging I just can't
figure out why it happens.

Greetings,
Stefan.


------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a 
definitive record of customers, application performance, security 
threats, fraudulent activity and more. Splunk takes this data and makes 
sense of it. Business sense. IT sense. Common sense.. 
http://p.sf.net/sfu/splunk-d2d-c1