Page 1 of 1

std:bad_alloc

Posted: Sun Jan 06, 2019 3:40 am
by BruceTheGoose
I suspected a memory leak in my game so I let it run for about an hour and I finally got a crash message ...
IMG_1079 (2).PNG
IMG_1079 (2).PNG (385.51 KiB) Viewed 3853 times
If anyone can provide an explanation for this error that would be awesome.

Re: std:bad_alloc

Posted: Sun Jan 06, 2019 4:09 am
by MrFariator
Can you show code?

Re: std:bad_alloc

Posted: Sun Jan 06, 2019 4:20 am
by BruceTheGoose
MrFariator wrote: Sun Jan 06, 2019 4:09 am Can you show code?
Here are the relevant code snippets:

I have a scroll frame object with a function that gets all of the visible objects :

Code: Select all

function Scroll:getVisibleObjects()
  local visible = {}

  for i = 1, #self.content.objects do
    if(self:checkCollision(self.content.objects[i])) then
      visible[#visible + 1] = i
    end
  end
  return visible
end
This function is called in the scroll frame's update function :

Code: Select all

function Scroll:update(dt, scrolling)

  self:handleScrollbar()

  if(self.content.height > self.height) then

    self:handleScrolling(dt)

    self.touched = self:getTouch()

    if(self.touched) then
      self:handleTouch(dt, scrolling)
    else
      self:handleRelease(dt)
    end
  end
  
  self.visible = self:getVisibleObjects()
  self.content:update(dt, self.visible)
end
This is the draw method for a button :

Code: Select all

function Button:draw()

  if(self.visible) then
    lg.setColor(0, 0, 0, self.shadowIntensity)
    lg.draw(Loader.gfx["boxshadow"], self.x + self.shadowOffsetX, self.y + self.shadowOffsetY, 0, self.shadowScaleX, self.shadowScaleY)

    lg.setColor(self.colorHover[1], self.colorHover[2], self.colorHover[3], self.alpha)
    lg.rectangle("fill", self.x, self.y + self.height * 0.55, self.width, self.height * 0.5, self.roundness, self.roundness) -- Line 97
    
    ......

Re: std:bad_alloc

Posted: Sun Jan 06, 2019 5:35 am
by MrFariator
Can't really spot anything that could be at fault in those snippets, because it doesn't really show the internal workings of your code. However, what does

Code: Select all

Loader.gfx["boxshadow"]
do? Is that the line 97 the game errors on?

Re: std:bad_alloc

Posted: Sun Jan 06, 2019 1:05 pm
by slime
std::bad_alloc is just a generic "out of memory" message – LÖVE's internal C++ code tries to allocate some memory but either the program has already run out of memory by that point, or you're making it allocate a massive amount of memory due to the arguments you pass in to the function that's outputting that error.

It's probably going to be really hard to see where your memory leak is coming from without a .love file.