Object destruction

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Sky Rider
Prole
Posts: 11
Joined: Mon Jun 18, 2012 9:07 am

Object destruction

Post 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?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Object destruction

Post 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.
Sky Rider
Prole
Posts: 11
Joined: Mon Jun 18, 2012 9:07 am

Re: Object destruction

Post 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 :(
kclanc
Citizen
Posts: 89
Joined: Sun Jan 29, 2012 6:39 pm

Re: Object destruction

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Object destruction

Post 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).
Help us help you: attach a .love.
Sky Rider
Prole
Posts: 11
Joined: Mon Jun 18, 2012 9:07 am

Re: Object destruction

Post 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.
kclanc
Citizen
Posts: 89
Joined: Sun Jan 29, 2012 6:39 pm

Re: Object destruction

Post 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
Sky Rider
Prole
Posts: 11
Joined: Mon Jun 18, 2012 9:07 am

Re: Object destruction

Post 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 :)
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests