Page 1 of 1

Object destruction

Posted: Fri Jun 22, 2012 4:37 pm
by Sky Rider
I've faced a weird problem: I cant destroy an object O_o
The thing is that I create almost every object as an instance of a class. So I cant set an object to nil directly, thats why I use such method:

Code: Select all

function physical:deinit()
	self = nil
end
But it doesnt work. I aslo tried:

Code: Select all

function physical:deinit()
	objList[self.id] = nil --objList contains all the objects, self.id as an index of an instance in the list
end
But it has no effect as well.
So how can I destroy an object?

Re: Object destruction

Posted: Fri Jun 22, 2012 5:22 pm
by bartbes
You remove all references.

If you're using love.physics (or possibly other physics engines), and you're witnessing the object staying in the simulation for a while, that's because garbage collection isn't instantaneous, love.physics has destroy methods on most objects, for this specific reason, and other physics engines might have similar methods.

Re: Object destruction

Posted: Fri Jun 22, 2012 5:44 pm
by Sky Rider
bartbes wrote:You remove all references.
Somewhy I dont. When, for example, I create an object "player"

Code: Select all

pl = player:new("EaJet", 250, 250, 180)
and then destroy it with

Code: Select all

self = nil
I can still access data in "pl" table, for eample, by drawing

Code: Select all

love.graphics.print(pl.module[1].x1, 25, 150)
Wth, "self = nil" should destroy the whole table :(

Re: Object destruction

Posted: Fri Jun 22, 2012 5:55 pm
by kclanc
self and pl, being local variables, are considered references. assigning self to nil just removes one reference; it does not affect the object being pointed to. Since pl is a reference and remains in tact, assigning self to nil does not remove all references to your object.

Re: Object destruction

Posted: Fri Jun 22, 2012 6:16 pm
by Robin
Sky Rider wrote:Wth, "self = nil" should destroy the whole table :(
No, it only changes a single reference. There's still at least one other reference: the pl table. You need to bind nil to that name as well (and make sure it doesn't try to draw the player if pl is nil).

Re: Object destruction

Posted: Fri Jun 22, 2012 6:31 pm
by Sky Rider
Of course, pl = nil solves all the problems :) But, for example, for bullets I have such code:

Code: Select all

if love.mouse.isDown("l") then
	bullet:new(hostPlayer) --hostPlayer is a parent object
end
So bullet:new does return a table, but it is ignored. Instead, bullet is added to objList:

Code: Select all

table.insert(objList, self)
self.id = table.maxn(objList)
and then, when its time to destroy a bullet (it comes after 1.5s of life), I simply remove it from the table:

Code: Select all

table.remove(objList, self.id)
-- shifting ids of other objects:
for j = self.id + 1, table.maxn(objList) do
	objList[j].id = objList[j].id - 1
end
self = nil
But memory doesnt seem to be freed. In fact, with every new bullet the game consumes more and more memory despite the fact, that every bullet lives 1.5s.

Re: Object destruction

Posted: Fri Jun 22, 2012 7:00 pm
by kclanc
If all references have been removed, the memory will not be freed until garbage collection happens. Calling collectgarbage("collect") will trigger this. http://www.lua.org/manual/5.1/manual.html#2.10

Re: Object destruction

Posted: Fri Jun 22, 2012 7:32 pm
by Sky Rider
Even calling collectgarbage("collect") in update function doesn't help. I guess there are some references I've forgotten about. Gonna check it tomorrow. Thanks :)