Jump to content
  • Advertisement
Sign in to follow this  
DrakeSlayer

luabind, virtual functions and smart pointers

This topic is 4857 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everybody I have a strange bug with luabind. I have a class luatest registered with lua bind
class luatest : public iObject
{
public:
	luatest()
	{};
	virtual bool doSomething(int a);
	bool hello()
	{
		return true;
	};
};

bool luatest::doSomething(int a)
{return true;
};


class_<luatest,Objptr<luatest> >("test")
			.def(constructor<>())
			.def("doSomething",&luatest::doSomething)
			.def("hello",&luatest::hello)




When i create an instance of luatest in lua and calls doSomething, my program crashes. it works if i call hello(). is this a bug in luabind ? thank in advance for your help

Share this post


Link to post
Share on other sites
Advertisement
I am not sure if it is a bug or if you are using it wrong... however the reason it works when you call hello() is because that function does not use any data associated with the class. I believe luabind is using an invalid pointer to your class (for whatever reason). Being virtual, the doSomething function must use the vtable, so an invalid class ptr would (could) cause a program crash in the case of a bad pointer.

Show your code for registering the class is luabind

Share this post


Link to post
Share on other sites
I first suspected an error in my reference counting code.
Then changed my code to use boost's smart pointers

namespace luabind
{
template<class T>
T* get_pointer(shared_ptr<T>& p) { return p.get(); }

template<class A>
shared_ptr<const A>* get_const_holder(shared_ptr<A>*)
{
return 0;
}
}

class luatest
{
public:
luatest()
{};
virtual bool doSomething(int a);
bool hello()
{
return true;
};
};

bool luatest::doSomething(int a)
{return true;
};



shared_ptr<luatest> createTest()
{
return shared_ptr<luatest>(new luatest);
};

void registerTest(lua_State* L)
{

module(L)
[
class_<luatest,shared_ptr<luatest> >("test")
.def("doSomething",&luatest::doSomething)
.def("hello",&luatest::hello),
def("createTest",&createTest)
];

};




And i still get the same bug.

Share this post


Link to post
Share on other sites
This works in cvs HEAD of luabind. Are you using beta6?

beta6 is really old now and the cvs HEAD is much more stable, and I would recommend using that instead.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!