views:

347

answers:

2

Having a problem with objects, not needed any more but still having references. Result: size of allocated memory is constantly growing due to not collected objects.

How to solve this sort of problem? Is there any way to find objects with only one reference, or objects with lifetime more than some value? Or any another solution?

Using Lua 5.1 and C++ with luabind.

Thanks.

A: 

I'm not certain about integrating it with C++ but it sounds like the garbage collector isn't being given an opportunity to run.

In your lua you could try explicitly invoking it and see if that helps. There is a function in the core apis collectgarbage(opt [, arg]).

Bryan McLemore
Garbage collection runs periodically and doing it's job well. But it does not collect objects with existing references. There is a lot of such objects and I can't find them.
kFk
Are you suffering from circular reference problems?
Bryan McLemore
Does lua solves circular references correctly? If no, it may be the reason.
kFk
Did you try using weak tables?
Dave Berk
I'm not sure if they do or not by default. I'm used to working inside of WoW which I think has a tweaked implementation of the garbage collector. Using weak references like Dave mentioned could help you get around it.
Bryan McLemore
Problem is to find where to use weak tables. Please see my answer in comments to egarcia.
kFk
+2  A: 

As someone is mentioning here, you can try using weak tables.

If you have some code like this:

myListOfObjects = {}
...
table.insert(myListOfObject, anObject)

Then once anObject stops being used, it will never be deallocated since myListOfObjects still references it.

You could try removing the reference in myListOfObjects (setting the reference to nil) but a simpler solution is declaring myListOfObjects as a weak table:

myListOfObjects = {}
setmetatable(myListOfObjects, { __mode = 'v' }) --myListOfObjects is now weak

Given that setmetatable returns a reference to the table it modifies, you can use this shorter idiom, which does the same as previous two lines:

myListOfObjects = setmetatable({}, {__mode = 'v' }) --creation of a weak table
egarcia
Weak tables is a good mechanism, but how to find proper objects to use in weak tables? There is about 2Mb of lua code in project and it's problematically to find what objects are not deallocated and still has a reference. Transforming all tables into weak tables will cause objects premature destroying. Also tables are mostly used to take ownership of objects and to free them only when it's necessary. Problem is that not all references are released and i cant' find them.
kFk
If you are not sure about what tables should be weak, then your only way of working is to set the all references to nil when they stop being useful.I recommend that you implement a "destructor" method for all your "classes" (types of tables). This destructor method should be called on objects that are not needed any more, and should equal their references to nil. Also, for "list of objects", implement a "liberate" method that assigns nil to references.
egarcia