[luabind] shared_ptr and nil
Brought to you by:
arvidn,
daniel_wallin
From: Jean-Claude M. <jc_...@em...> - 2008-11-18 13:51:02
|
Hello, When I export a class using shared_ptr, my lua script can crash the application if it calls a method of object that is "NULL". Here is some test code to illustrate that crash: Class to export: class TestClass { double val; public: boost::shared_ptr<TestClass> FindNode( const std::string &nodeName ) { // always return NULL for that test, in the real class it only returns NULL if it couldn't find the node. return boost::shared_ptr<TestClass>(); } void DoSomething( double v ) { val = v; } }; luabind export: luabind::module(m_luaState) [ luabind::class_<TestClass, boost::shared_ptr<TestClass>>("TestClass") .def(luabind::constructor<>()) .def("FindNode", &TestClass::FindNode) .def("DoSomething", &TestClass::DoSomething) ]; lua script: test = TestClass() test:DoSomething(0.0) node = test:FindNode("test") node:DoSomething(0.0) When I call this small test script, the variable node is an emptry(NULL) shared_ptr. When the script calls the DoSomething method, it extecutes the DoSomething with the "this" pointer at adress 0, therefore the application crashes. It souldn't be possible to crash the application from an external script. If I export the class with raw pointers (see example below), the script aborts with the following message: "script.lua:4: attempt to index global 'node' (a nil value)" This is the behaviour I would like to have (abort script instead of application crash). However, I need shared_ptr. Is it possible get the same behaviour with shared_ptr? Did I do something wrong in my luabind usage? I'm new to luabind. Same class, but with raw pointers (abort instead of application crash with the same script): class TestClass { double val; public: TestClass* FindNode( const std::string &nodeName ) { return NULL; } void DoSomething( double v ) { val = v; } }; luabind::module(m_luaState) [ luabind::class_<TestClass>("TestClass") .def(luabind::constructor<>()) .def("FindNode", &TestClass::FindNode) .def("DoSomething", &TestClass::DoSomething) ]; -- Jean-Claude Monnin jc_...@em... |