I’d like remove the objects from memory.
The memory isn’t being cleaned up. For example : If i type :
print(someObjectOfMyGroup) It should print ‘nil’ but this reference isn’t released.
local \_M = {} function \_M.new() local group = display.newGroup() local player = display.newSprite... group:insert(player) local function TouchFunction ... local function CollisionFunction... local function FreeMemory() for j=group.numChildren, 1, -1 do display.remove(group[group.numChildren]) group[group.numChildren] = nil end display.remove(group) group = nil --this shouldn't print 'nil' ? print(player) end player.touch = TouchFunction player:addEventListener("touch", player) player.collision = CollisionFunction player:addEventListener("collision", player) return group end return \_M
i thought in put all my objects in a table and remove it like so :
local myTable = {} myTable.player = display.newSprite ... --After remove this way : for j=1, #myTable do display.remove(myTable[j]) myTable[j] = nil end myTable = nil
But i don’t know if it would let the game more slow … what do you think ?
If you’re expecting “player” to be nil, it won’t. When you insert player into a group, you are inserting the object referenced by player into the group and not the variable itself. When you removed the object and set the value to nil in your for loop, it removed the object (which converted it to a table) and cleared the value in the group, but didn’t touch the value stored in “player”. What you need to do is set “player = nil” after you insert it into the table.
In the first code sample, I notice you’ve put the “player” into the group, but you’ve also placed listeners on the player object (“touch” and “collision”). Where do you remove those listeners? Somewhere else in the code that you didn’t include?
In the second code sample, that’s a reasonable approach, except that in your example, you’re adding the player as a dictionary item in the table (named “player”), and then when you clear you’re attempting to loop through by index numbers. It will not clear the items that way.
It shouldn’t. In fact, it might actually speed things up in the places where you’re clearing out these tables. By having them in one organized table, you can localize that table to the clear function, and the loop will perform faster. Likely though you won’t notice any improvement… but certainly, don’t worry about this slowing your game down.
I will note, however, that the “pairs()” function is slower than the indexed number method, simply by Lua performance. I suggest you do not use this clear function in a time-critical game loop cycle. Hopefully by your design, this can occur between scenes or somewhere that the player won’t notice any potential frame rate skip.
I’d like remove the objects from memory.
The memory isn’t being cleaned up. For example : If i type :
print(someObjectOfMyGroup) It should print ‘nil’ but this reference isn’t released.
local \_M = {} function \_M.new() local group = display.newGroup() local player = display.newSprite... group:insert(player) local function TouchFunction ... local function CollisionFunction... local function FreeMemory() for j=group.numChildren, 1, -1 do display.remove(group[group.numChildren]) group[group.numChildren] = nil end display.remove(group) group = nil --this shouldn't print 'nil' ? print(player) end player.touch = TouchFunction player:addEventListener("touch", player) player.collision = CollisionFunction player:addEventListener("collision", player) return group end return \_M
i thought in put all my objects in a table and remove it like so :
local myTable = {} myTable.player = display.newSprite ... --After remove this way : for j=1, #myTable do display.remove(myTable[j]) myTable[j] = nil end myTable = nil
But i don’t know if it would let the game more slow … what do you think ?
If you’re expecting “player” to be nil, it won’t. When you insert player into a group, you are inserting the object referenced by player into the group and not the variable itself. When you removed the object and set the value to nil in your for loop, it removed the object (which converted it to a table) and cleared the value in the group, but didn’t touch the value stored in “player”. What you need to do is set “player = nil” after you insert it into the table.
In the first code sample, I notice you’ve put the “player” into the group, but you’ve also placed listeners on the player object (“touch” and “collision”). Where do you remove those listeners? Somewhere else in the code that you didn’t include?
In the second code sample, that’s a reasonable approach, except that in your example, you’re adding the player as a dictionary item in the table (named “player”), and then when you clear you’re attempting to loop through by index numbers. It will not clear the items that way.
It shouldn’t. In fact, it might actually speed things up in the places where you’re clearing out these tables. By having them in one organized table, you can localize that table to the clear function, and the loop will perform faster. Likely though you won’t notice any improvement… but certainly, don’t worry about this slowing your game down.
I will note, however, that the “pairs()” function is slower than the indexed number method, simply by Lua performance. I suggest you do not use this clear function in a time-critical game loop cycle. Hopefully by your design, this can occur between scenes or somewhere that the player won’t notice any potential frame rate skip.