Lost Links in Lua

There is a problem with objects that do not require more, but still have links. Result: the size of the allocated memory is constantly growing due to unassembled objects.

How to solve this problem? Is there a way to find objects with a single link, or objects with a lifetime longer than some value? Or any other solution?

Using Lua 5.1 and C ++ with luabind.

Thanks.

+3
source share
2 answers

As mentioned here, you can try weak tables .

If you have a code like this:

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

anObject , , myListOfObjects .

myListOfObjects ( nil), myListOfObjects :

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

, setmetatable , , , :

myListOfObjects = setmetatable({}, {__mode = 'v' }) --creation of a weak table
+5

++, , .

lua , . apis collectgarbage(opt [, arg]) .

+1

Source: https://habr.com/ru/post/1725963/


All Articles