Jump to content
  • Advertisement
Sign in to follow this  
lukesmith123

Memory leaks when using luabind with lua

This topic is 2402 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

Hi,

I followed a tutorial in the book 'programming game AI by example' which teaches an introduction to scripting with lua.

I'm getting some unexpected memory leaks which I cant understand how they can happen. But as I am very new to lua and luabind I'm guessing that I might be missing something obvious here.

I register functions from the statemachine and bot class in the bot class constructor (lua_close is called in the bot class destructor):


lua = luaL_newstate();
luabind::open(lua);
luaL_openlibs(lua);

luabind::module(lua)
[
luabind::class_<StateMachine<Bot>>("StateMachine")
.def("ChangeState", &StateMachine<Bot>::ChangeState)
.def("CurrentState", &StateMachine<Bot>::CurrentState)
.def("SetCurrentState", &StateMachine<Bot>::SetCurrentState)
];

luabind::module(lua)
[
luabind::class_<Bot>("Bot")
.def("one", &Bot::one)
.def("two", &Bot::two)
.def("GetStateMachine", &Bot::GetStateMachine)
];

luaL_dofile(lua,"lua.lua");
luabind::object states = luabind::globals(lua);

if (luabind::type(states) == LUA_TTABLE)
{
stateMachine->SetCurrentState(states["State_one"]);
}



And the statemachine class looks like this:



template <class entity_type>
class StateMachine
{
private:
entity_type* Owner;
luabind::object currentState;
public:
StateMachine(entity_type* owner):Owner(owner){}
void SetCurrentState(const luabind::object& s){currentState = s;}
void UpdateStateMachine()
{
if(currentState.is_valid())
{
(currentState)["Execute"](Owner);
}
}
void ChangeState(const luabind::object& new_state)
{
(currentState)["Exit"](Owner);
currentState = new_state;
(currentState)["Enter"](Owner);
}
const luabind::object& CurrentState()const{return currentState;}
};



Everything works as expected but I am left with 6 memory leaks when I exit the program.

Also when I remove the luabind::module code where the classes are registered then the memory leaks dissapear.

Does anybody have any ideas?

thanks,

Share this post


Link to post
Share on other sites
Advertisement
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!