Love2d stops running after awhile

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

Re: Love2d stops running after awhile

Post by Gunroar:Cannon() »

Thanks , I fixed it after vigorous debugging and you're point REALLY helped.
It seemed some things kept getting removed because the _destroy function was the same as their new _destroy when an object that had been used before gets used again.
(Classic, an infinite loop just like my signature :P )
I'll check the code now and see if it doesn't close, I might be back with more code if it does...

Code: Select all

--class.lua modified from rotlove's class

local BaseClass = {timeMade=-1}
CLASSES = {BaseClase=BaseClass}
POOLS = {}
local function log(_str,file)
--asssert(_str~=5)
    local file = file or _LOG_FILE
    local str = _str--lume.wordwrap(inspect(_str,1),90)
    file:write(str.."\n")
    return true
end



local function destroyFunc(self,...)
    local notActive = POOLS[self.__class].notActive local te = #notActive
    local te = #notActive
    --log(" pool at "..self.__id.." is being freed."..#notActive)
    --assert(self.__active,self.__id)
    self.__active = false
    notActive[#notActive+1] = self.__id
    assert(#notActive<=self.poolSize,#notActive)

    local re = self._destroy(self,...)
    assert(#notActive-te==1,self.class)
    return re
end

local function oldNew(self,...)
    local t = setmetatable({}, self)
    t:__init__(...)
    local kw = ...
    t._kwargs = t._kwargs or t.kwargs or (kw and type(kw)=="table" and kw)
    t.timeMade = love.timer.getTime()
    if store then
        store(t)
    end
    return t
end

function BaseClass:new(...)
    local t = setmetatable({}, self)
    t:__init__(...)
    local kw = ...
    t._kwargs = t._kwargs or t.kwargs or (kw and type(kw)=="table" and kw)
    t.timeMade = love.timer.getTime()
    if store then
        store(t)
    end
    return t
end

function BaseClass:extend(name, t, tt)
    assert(name, "Class must have a name!")
    
    local p = t
    if type(t)~="table" then
        t = nil
    else
        p = tt
    end
    
    t = t or {}
    if type(p)=="number" then
        t.poolSize = p
    end
    t.__index = t
    t.__class = name
    t.class = name
    t.super = self
    t.__data = t.__data or t.data or {
        x=0,y=0,w=0,h=0,_w=0,_h=0,source="none.png",
        active=false,attack=0,life=0,passive=true
    }
    
    t.poolSize = t.poolSize or 1000
    t.pool = p
    
    local tclass = setmetatable(t, { __call = self.new, __index = self })
    tclass.__lt = self.__lt

    CLASSES[name] = tclass
    t["is"..name] = true
    
    if not p then
        return tclass
    end
    t.__data.map = game.map
    --log("pooling "..name)
    

    t = tclass
    
    local poolSize = t.poolSize
    local notActive = {}
    local pool = {}
    if not self.__taggezd then
        --so won't get repeatedly added to notActive
        --from inheritance though does not work if 
        --super form is not called
        
      --  t.__tagged = true
        local destroy = t.destroy or null
        t._destroy = destroy
        t._hasD = destroy~=null
        function t:destroy(...)
            --log(name.." pool at "..self.__id.." is being freed.")
          --  if self.isPuff then log(string.format("trying to destroy...%s",#notActive)) end
            notActive[#notActive+1] = self.__id
        --    if self.isPuff then log("done trying now...") end
            return destroy(self,...)
        end
        
    end
    
    --populating pool
    local pp
    local notYet = true
    for i = 1, poolSize do
        pool[i] = t:new(t.__data)
        pool[i].__id = i
        pool[i]:destroy()
        pool[i].passive = false
        pp=i
    end
    if t._hasD then
        t._hasD = nil
        t.destroy = t._destroy
    end
    
    local nonTotal = #notActive
    
    local function new(self,...)
        assert(#notActive>=1,"No space left in pool:size "..nonTotal.."!")
        if self.class~=name then
            return oldNew(self,...)
        end
        local r = table.remove(notActive,1)
        
        --log(name.." is making at "..r)
        local obj = pool[r]
        obj:__init__(...)
        obj.__active = true
        obj.destroyed = false
        obj.dead = false
        
        if not obj.__tagged then
            assert(obj.destroy~=destroyFunc,r)
            obj._destroy = obj.destroy
            obj.destroy = destroyFunc
            obj.__tagged = true
        end
        
        local kw = ...
        obj._kwargs = obj._kwargs or obj.kwargs or (kw and type(kw)=="table" and kw)
        obj.timeMade = love.timer.getTime()
        if store then
            store(obj)
        end
        return obj
    end
    
    t.new = new
    
    
    
    POOLS[name] = {pool=pool,notActive=notActive}
    
    return tclass
end

function BaseClass:__init__()
end


function BaseClass.__lt(self,b)
    return self.timeMade<b.timeMade
end

function BaseClass:__le(self,b)
return true end

function BaseClass:callSuper(func,...)
    if not self.super[func] then
        return
    end
    return self.super[func](self,...)
end

return BaseClass
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: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: Love2d stops running after awhile

Post by Gunroar:Cannon() »

Is there a chance for love.lowmemory not to be called before the app closes because it closed once and was called but then closed again(and majority of the time) was not called. The game doesn't really freeze before closing (again, thnx for the advice). So this question is just to make sure of something pls.thnx.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Love2d stops running after awhile

Post by slime »

I'm not sure I'm fully parsing the sentence, but love.lowmemory is never guaranteed to be called - mobile operating systems just send it as a "you're already too late but here's what's going on" kind of warning and it may or may not be read before the OS terminates the app (especially if lots of memory is allocated in a single frame, in which case the OS may terminate the app or the app will crash out of memory within that frame, and there will be no chance for the next frame to receive events from the OS).
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: Love2d stops running after awhile

Post by Gunroar:Cannon() »

Wow!!! You don't know how much I appreciate that answer, Mr solid snayke sir!! That should mean my game is fixed(for now)...until next time, THNXXX.
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], Majestic-12 [Bot] and 34 guests