Jump to content
• ### What is your GameDev Story?

• Advertisement

# Lua/ToLua - Memory leak

This topic is 3831 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've recently made use of ToLua to export my Vector and Matrix classes for use in Lua scripts. After much messing around with my new toy, my swap disk started to go crazy and my game would segfault. After some testing with a system activity monitor, it revealed my game's memory size grew approx. 1MB every second. I've narrowed the problem down to ToLua, and after some Googling I think it's something to do with "tag methods". Here's some example code: Vector class (C++):
class SYM_VECTOR3
{
public:

float x;
float y;
float z;

SYM_VECTOR3() { x = y = z = 0.0f; }

SYM_VECTOR3(float _x, float _y, float _z)
{
x = _x;
y = _y;
z = _z;
}

etc...


Matrix class (C++):
class SYM_MATRIX4
{
public:
//Constructor
SYM_MATRIX4() { Reset(); }

SYM_MATRIX4(float *Matrix)
{
memcpy(_Matrix, Matrix, 16 * sizeof(float));
}

...

SYM_VECTOR3 RotateVector(SYM_VECTOR3 Vector)
{
return SYM_VECTOR3( Vector.x * get(0, 0) + Vector.y * get(1, 0) + Vector.z * get(2, 0),
Vector.x * get(0, 1) + Vector.y * get(1, 1) + Vector.z * get(2, 1),
Vector.x * get(0, 2) + Vector.y * get(1, 2) + Vector.z * get(2, 2));
}

inline float get(const int col, const int row) const
{
return _Matrix[col * 4 + row];
}

//The 4x4 matrix
float _Matrix[16];
};


A troublesome Lua script:
-- Experimental hovercraft script

-- Get matrix
Matrix = getmatrix("HCraft")

-- Get force direction in global space
-- MEMORY GROWS IN SIZE HERE
ThrusterDir = Matrix:RotateVector(SYM_VECTOR3:new(0, 1, 0))


To generate the binding Lua script, I use this command:
tolua -o lua_matrix.cpp -H lua_matrix.h -n sym_matrix matrix.h
tolua -o lua_vector.cpp -H lua_vector.h -n sym_vector vector.h

Any help appreciated

#### Share this post

##### Share on other sites
Advertisement
After some reading, I've discovered this:

Quote:
 User defined types All other types that appear in the package file being processed are considered user defined types. These are mapped to tagged userdata type in Lua. Lua can only store pointers to user defined types; although, tolua automatically makes the necessary arrangement to deal with references and values. For instance, if a function or method returns a value of user defined type, tolua allocates a clone object when returning it to Lua and sets the garbage collection tag method to automatically free the allocated object when no longer in use by Lua.

How can I pass a user type value without these pointers building up? Here's a few examples of how I've tried getting it to work:

Works for testing purposes, but memory obviously builds up:
SYM_MATRIX4 *Matrix = new SYM_MATRIX4(Engine->Scene.GetGeometry(lua_tostring(LuaState, 1))->GetMatrix());tolua_pushusertype(LuaState, Matrix, "SYM_MATRIX4");

Gives invalid results on the Lua end:
tolua_pushusertype(LuaState, (void*)&Engine->Scene.GetGeometry(lua_tostring(LuaState, 1))->GetMatrix(), "SYM_MATRIX4");

Any ideas?

#### Share this post

##### Share on other sites
I haven't tired toLua myself, instead I'm using MLuaBind and tried this test out, it's not leaking any memory even after a million iterations of this. I suggest using MLuaBind, its a modern template Lua/C++ binding library and it's excellent! I got it working in minutes and it auto detects parameters and binding for funcitons and methods.

http://sourceforge.net/projects/mluabind

You'll need Loki, a support library.

http://sourceforge.net/projects/loki-lib/

Try it out!

-ddn

#### Share this post

##### Share on other sites
I believe the problem lies in calling SYM_VECTOR3:new(0, 1, 0) rather than just doing SYM_VECTOR3(0, 1, 0). Tolua sets up a collection scheme so that temporary and local objects are collected using the tag methods for new and delete, as long as they are constructed using, ie, SYM_VECTOR3() instead of explicitly calling the new function. The new function doesn't mark the created object as being managed by the garbage collector. The function SYM_VECTOR3() does something similar to this:

function create_SYM_VECTOR3(x,y,z)
local obj=SYM_VECTOR3:new(x,y,z)
tolua.takeownership(obj)
return obj
end

The takeownership part is the key part; otherwise, it's pretty much just a raw new call, and what you are doing is tantamount to performing raw new calls in straight C++, without deleting.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• ### Popular Now

• 15
• 14
• 46
• 22
• 27
• Advertisement
• ### Forum Statistics

• Total Topics
634048
• Total Posts
3015237
×

## Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!