Corona Simulator has started crashing consistently

I downloaded Corona a few months ago and have been making a game.  I have had no issues whatsoever with the Corona simulator crashing until just recently.  I started noticing that it would occasionally crash (maybe 1 or 2 times every couple of hours).  

Today, it is now crashing consistently every time I get to my main gameplay screen. 

I added memory monitoring code to my main like this:

local monitorMem = function() collectgarbage() print( "\nMemUsage: " .. collectgarbage("count") ) local textMem = system.getInfo( "textureMemoryUsed" )/1000000 print( "TexMem: " .. textMem) end Runtime:addEventListener("enterFrame", monitorMem)

When I am in the main game screen, the numbers are staying consistent and not rising at all. 

14:06:26.063  MemUsage: 1391.57421875

14:06:26.063  TexMem: 14.198268

14:06:26.084  

14:06:26.084  MemUsage: 1390.87890625

14:06:26.084  TexMem: 14.198268

14:06:26.101  

14:06:26.101  MemUsage: 1391.59765625

14:06:26.101  TexMem: 14.198268

14:06:26.117  

14:06:26.117  MemUsage: 1392.31640625

14:06:26.117  TexMem: 14.198268

14:06:26.135  

14:06:26.135  MemUsage: 1392.31640625

14:06:26.135  TexMem: 14.198268

14:06:26.150  

14:06:26.150  MemUsage: 1392.34375

14:06:26.150  TexMem: 14.198268

14:06:26.173  

14:06:26.173  MemUsage: 1390.85546875

14:06:26.173  TexMem: 14.198268

It stays around 1390 and 14.1 the entire time it is in the game screen until it crashes without going up. 

The crashes seem to happen anywhere from as fast as 5 seconds into my game play , to as long as 1 minute.   Some take around 30 seconds. 

Recently, I have been changing some of code relating to restarting my level.  So I’ve been setting up key presses to remove all enemies and then another key press to restart the level by resetting everything and respawning the enemies.  

It seems like the crashes started happening more and more while I was tweaking the code.  However, I think I have the code fixed now (based off the MemUsage and TexMem) but now the crashes are happening every single time.

I tried restarting the laptop. 

I also know that recently (a few days ago I think), Windows did an automatic update, so I’m not sure if something got screwed up because of that. 

I would post code but I have lots of modules and hundreds of lines of code so I’m not sure what to even post.  

It’s weird because of 90% of my code has been the same for the past several weeks and I was not getting any of these crashes until just recently. 

Any tips on what I can try?

Can you upload your project so we can try it on our Windows and Mac machines? That will tell you if it’s your machine or the project.

Whats the process for uploading a project?  Haven’t done that before. 

Well I would just whack it on Google Drive or Dropbox or similar and either share a link to the folder on here, or send the link directly to anyone who offers to try running it.

There are some custom assets I don’t really want to share if I don’t have to, but I will copy it over to my other desktop pc and see if it crashes like it does on this laptop.  I’ll post back after with an update.  

Do you have any crash logs?

I copied it over to my desktop and the second time playing through the game scene, it crashed as well.  So I think it’s safe to say it’s not something with my machine.  

Do I just try backing out changes and see if I can find some piece of code that is causing the problem?  

@agramonte             When it crashes and I have to force close the simulator, the only thing that shows up in the console is:

The stdin connection has closed.

Does it actually close the simulator or just hang? Are you sure you’re not causing an infinite loop somewhere?

It just hangs.  The simulator screen just freezes and gets grayed out like when apps stop responding.  

If I did somehow cause an infinite loop, is that what the simulator would do?  Just immediately freeze?   

Are there any other metrics I should be checking besides MemUsage and TexMem?  

I can’t imagine it’s a memory issue, it’s a lot harder to run out of memory on the simulator than a real device.

Have you got any while loops or anything like that, or anything happening in an enterFrame listener that you thought happens once but actually happens every frame?

Difficult to help further without any code, but certainly sounds like you’re locking it up somehow.

I was able to narrow down the problem.  If I remove the enemies from the game scene, it does not crash. 

Also, I do have a while loop in my enemy movement code.  I’ve had it there for a while and it never caused an issue before, but I have been making some tweaks, so maybe I broke it somehow?  

This is my enemy code: 

local M = {} local my\_globals = require "scripts.my\_globals" local physics = require "physics" local oss = my\_globals.orange\_sprite\_size local jump\_speed = my\_globals.orange\_jump\_speed local jump\_dist = my\_globals.orange\_jump\_dist local sheetOptions = { width = 128, height = 128, numFrames = 2 } local sheet = graphics.newImageSheet("sprites/orange\_sprite\_sheet.png", sheetOptions) local sequences = { { name = "jump", frames = { 1,2,1 }, time = jump\_speed, loopCount = 1 }, { name = "stand", frames = { 1 } } } function M.create(dsp\_grp, enem\_box) local enemies = {} local enemy\_box = enem\_box local bounds = enemy\_box.contentBounds local xMin = bounds.xMin local xMax = bounds.xMax local yMin = bounds.yMin local yMax = bounds.yMax local num\_enemies = enemy\_box.num\_of\_enemies local p\_body\_size = my\_globals.orange\_sprite\_size.x local mv\_min = my\_globals.orange\_min\_mv\_time local mv\_max = my\_globals.orange\_max\_mv\_time local my\_shape = { 0,-p\_body\_size/3, p\_body\_size/2.3,p\_body\_size/12, p\_body\_size/3.6,p\_body\_size/1.8, -p\_body\_size/3.6,p\_body\_size/1.8, -p\_body\_size/2.3,p\_body\_size/12 } local function getMoveLocation(self) local xOffset = math.random(-jump\_dist, jump\_dist) local yOffset = math.random(-jump\_dist, jump\_dist) local newX = self.x + xOffset local newY = self.y + yOffset while( newX \< xMin or newX \> xMax or newY \< yMin or newY \> yMax) do xOffset = math.random(-jump\_dist, jump\_dist) yOffset = math.random(-jump\_dist, jump\_dist) newX = self.x + xOffset newY = self.y + yOffset end return newX, newY end local function getSpawnLocation() local xLoc = math.random(xMin, xMax) local yLoc = math.random(yMin, yMax) return xLoc, yLoc end for i = 1, num\_enemies do local enemy = display.newSprite(sheet, sequences) function enemy:goodBye() transition.cancel(self) timer.cancel(self.timerID) display.remove(self) self = nil print("i was removed") end function enemy:timer(event) local newX, newY = getMoveLocation(self) self:play() transition.to(self, {time = jump\_speed, x=newX, y=newY}) self.timerID = timer.performWithDelay(math.random(mv\_min, mv\_max), enemy) end enemy.x, enemy.y = getSpawnLocation() enemy.timerID = timer.performWithDelay(math.random(mv\_min, mv\_max), enemy) physics.addBody(enemy, {shape=my\_shape, bounce=0}) enemy.isFixedRotation = true enemy.name = "enemy" enemies[i] = enemy dsp\_grp:insert(enemy) end return enemies end return M

Do you see anything in here that could cause it to lock up?  It doesn’t always happen at the same time, but I do have a lot of random movement going on, so I’m wondering if at some point the enemy chooses a particular location that causes the problem. 

What I do when I get a situation like that is keep track of how many times the loop has run, and bail out if it gets to say 50, 100 - whatever seems unreasonable. I’ll also print out the relevant values for each iteration of the loop to see why it continually meets the while loop conditions. By bailing out the app will remain responsive and will print to the console so you can work out what’s wrong.

[lua]

local iterations = 0

while iterations < 100 or myOtherConditions do

   iterations = iterations + 1

end

print (allMyVariables…" so I can see if something is way out of whack")

end

[/lua]

First off, you were right about the infinite loop.  I have boxes that I’m using as boundaries for the enemies.  They are never supposed to move out of the boxes. 

However, occasionally if one enemy jumps into another, it might push that enemy out of the box and then the code which tries to move the enemy will break.  It breaks because enemies are only allowed to move a certain distance, and if they get pushed more than that distance out, it won’t work. 

That being said…  I’m not really sure how to fix it other than making all the enemies (isSensor = true) which I was trying to avoid.  

Could I bump up the friction a lot or something along those lines so they won’t be pushed out of the boundaries when hit by another enemy?

Actually was able to solve the problem by adding a check before that while loop starts to see if the enemy is outside the boundaries or not, and if so, don’t even start the loop just move him closer to the box. 

@nick sherman   Really appreciate the help pointing me in the right direction to get this figured out!  Good advice trying it on the other pc and figuring out the infinite loop.   :slight_smile: