Free memory correctly...?

I wonder how to free memory if an image is used as a reference? Like for example:

character=newImage... AttackPack.activeCharacter=character WildBunch.lastMember=character

I guess now it isn’t enough to do…

display.remove (character) character = nil

right?

How can I make sure to really free the complete memory for the image here?

And how do I have to do this for other stuff than images, like for example group references, sounds, tables?

Thx for your help!

you must release ALL references to ANY such object before Lua will consider it for garbage collection.

your “AttackPack” and “WildBunch” objects are potentially still hanging on to a reference (assuming they haven’t changed)

fe, insert just prior to nil’ing character:

if (wildBunch.activeCharacter==character) then

  WildBunch.activeCharacter=nil

end

– and so on,etc

Thx for your fast feedback!

One more question:

How do I have to handle function calls using the character from above then, like for example this one:

local moveObject moveObject = function (object)       object.x=object.x+1 end moveObject (character)

or like this one…

local moveObject2 moveObject2 = function (object)      local myObject=object      myObject.x=myObject.x+1 end moveObject2 (character)

Is there something to think of regarding memory, other than to destroy “character” later?

the extra local in the second form is unnecessary (and in fact a bit wasteful), all you need is:

local function moveObject(obj)

  obj.x = obj.x + 1

end

moveObject(character)

– or equivalent OOP style:

function character:move()

  self.x = self.x + 1

end

character:move()

neither form has any intrinsic memory issues to worry about (when character is nil’ed, first form’s function will remain, second form’s function will be released along with object;  either is fine, just depends on how you might be reusing them for other objects, or not)

hth

Thank you for the explanation!

you must release ALL references to ANY such object before Lua will consider it for garbage collection.

your “AttackPack” and “WildBunch” objects are potentially still hanging on to a reference (assuming they haven’t changed)

fe, insert just prior to nil’ing character:

if (wildBunch.activeCharacter==character) then

  WildBunch.activeCharacter=nil

end

– and so on,etc

Thx for your fast feedback!

One more question:

How do I have to handle function calls using the character from above then, like for example this one:

local moveObject moveObject = function (object)       object.x=object.x+1 end moveObject (character)

or like this one…

local moveObject2 moveObject2 = function (object)      local myObject=object      myObject.x=myObject.x+1 end moveObject2 (character)

Is there something to think of regarding memory, other than to destroy “character” later?

the extra local in the second form is unnecessary (and in fact a bit wasteful), all you need is:

local function moveObject(obj)

  obj.x = obj.x + 1

end

moveObject(character)

– or equivalent OOP style:

function character:move()

  self.x = self.x + 1

end

character:move()

neither form has any intrinsic memory issues to worry about (when character is nil’ed, first form’s function will remain, second form’s function will be released along with object;  either is fine, just depends on how you might be reusing them for other objects, or not)

hth

Thank you for the explanation!