Thoroughly deleting an object

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
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Thoroughly deleting an object

Post by Gunroar:Cannon() »

I want to delete a map object that holds a bunch of entities and I have an idea but it still leaves the map object floating around with a few mb after I destroy it and the framerate goes down a little too.
1.Please just check it over if this is ok and notify if you see any possible problem( I use hump's gamestate (https://github.com/vrld/hump/blob/master/gamestate.lua)so do I need to do anything specialwith that)
2.Is there anyway to check whether an object is still referenced and where?.
Here's the destroy function( I destroy objects so space will be freed in the object pool for that object):

Code: Select all

--GS = hump.gamestate btw

function Map:destroy(destroyPeer)
    self.isOld = true
    
    --destroying entities to free up space in
    --object pools
    local ents = lume.copy(self.entities)
    for x, i in pairs(ents) do
        if i ~= game.player then
            i:destroy()
        end
    end
    
    local bulls = lume.copy(self.bullets)
    for x, i in pairs(bulls) do
        i:destroy()
    end
    
    self.entites = {}
    self.bullets = {}
    
    --destroying tiles
    self.allTiles = {}
    self.tiles = {}
    self.grid = {}
    
    GS.stacks = {}--??
    
    game.map = nil
    game.currentMap = nil
    self.active = false
    self:unpause()
    self.player.map = nil
    game.player = nil
    self.player = {}

    lume.clear(self)
end

The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Thoroughly deleting an object

Post by Gunroar:Cannon() »

I also use bump.lua for entities and all tiles.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

Re: Thoroughly deleting an object

Post by 4vZEROv »

A table will only be garbage collected if there is no reference of it anywhere is the program. (not counting weak tables)
So a reference to your map is still somewhere in your code and keep it alive.

It's probably inside your bump.lua code, I believe you initialize the bump tables with some references if I remember correctly.
User avatar
dusoft
Party member
Posts: 492
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Thoroughly deleting an object

Post by dusoft »

See: https://www.lua.org/pil/17.html
call: collectgarbage()
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Thoroughly deleting an object

Post by Gunroar:Cannon() »

Ah, I found it. Hump gamestate had a local called stacks so it couldn't be accessed and the class couldn't be deleted. I accessed it by making it part of the Gamestate class. thnx.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Thoroughly deleting an object

Post by Gunroar:Cannon() »

I made this function to find any references to an object (which I still had due to some accidentally global declared tables) so you can flush it out, and that really fixed it for me https://love2d.org/forums/viewtopic.ph ... 3d7354eba0
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
Post Reply

Who is online

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