Main.lua Running Twice

We’ve finished an app that runs as it should in the simulator, and have built it to a device for final testing (we’ve built to both a Nook and a Kindle and had this same issue)

Main.lua is running twice, and so all of our variables are getting created twice, all the event listeners are created twice, and so on… obviously this is not how we want the app to run.

We’ve commented out everything in main.lua except for a print statement and that print statement prints twice as soon as the app loads on device and runs main.lua twice.

We’ve uninstalled Corona and reinstalled, rebooted the computer and the Nook device several times, copied out the text from main.lua and created it in a blank new .lua file, same results - main.lua runs just once in the simulator, but twice through on the device. We’re not using Cider or Glider which I’ve seen in the forums can sometimes cause an issue. We’re using the latest stable build Corona 971.

The real fix would be for main.lua NOT to run twice, but since we’ve tried everything to no avail we decided on a workaround:

if (settings.alreadyRan) then
  print(“NOT RUNNING MAIN AGAIN”)
else

  – all of our relevant main.lua code here

  settings.runOnce()

end

IN SETTINGS.LUA:

function runOnce (event)
  if (alreadyRan) then
     print “ALREADY RAN”
  else
     print “FIRST TIME RUNNING”
     alreadyRan = true
  end
end

The idea was that the second time main.lua ran through, settings.alreadyRan would be true and therefore nothing would run again. We basically put all of our code in the ‘else’ so that it runs the first time but won’t be run a second time. Thought this would work to prevent main.lua from reloading all our assets when it runs the second time through, but all of a sudden we can’t access our functions in Settings (which we have been accessing all along throughout this app and all our other apps and worked just fine until we tried it here).

In the simulator settings.runOnce() works as expected and prints “FIRST TIME RUNNING”

On the device we get this error:

Lua Runtime Error: lua_pcall failed with status: 2, error message is: ?:0: attempt to call field ‘runOnce’ (a nil value)

Any ideas why main.lua is running twice or why we’re getting this runtime error when trying to call our workaround function in settings would be greatly appreciated…

Are you sure it is running twice on the Device and not the simulator. I have seen this behavior of double printing and running when running the the simulator and debugging with a 3rd part tool like Lua Glider or others.

something to do with the hook into java for debugging I guess.

But I have never seen it run twice on the actual device.

Larry Meadows

can you post your main.lua?

We believe the cause was a call to “require ‘main’” at the top of our settings.lua file, apparently that was enough for the program to run through all the code in main.lua again

That would do it!

Are you sure it is running twice on the Device and not the simulator. I have seen this behavior of double printing and running when running the the simulator and debugging with a 3rd part tool like Lua Glider or others.

something to do with the hook into java for debugging I guess.

But I have never seen it run twice on the actual device.

Larry Meadows

can you post your main.lua?

We believe the cause was a call to “require ‘main’” at the top of our settings.lua file, apparently that was enough for the program to run through all the code in main.lua again

That would do it!

I made that mistake a long time ago.  I went into Corona and Walter found the issue.  I was like OMG how was I so stupid!!! LOL nice job glad you got it working :slight_smile:

I made that mistake a long time ago.  I went into Corona and Walter found the issue.  I was like OMG how was I so stupid!!! LOL nice job glad you got it working :slight_smile: