How to hunt down the source of 'attempt to perform arithmetic' error?

@cinetek: I am not called either of the above methods, but i think i’m not cancelling transitions properly. So i’ll give that a go.

Thanks

Are you seeing this in a dialog box that’s popping up or are you seeing in it in the console output/log?

@rob: I am seeing it in both. I’m using windows simulator by the way. corona build 1215.

Then there should be more to the error than that one line.  There should be a whole stack trace that it dumps out.  Also there may be warnings a couple of lines before it.  Since that error isn’t giving you line numbers, you are passing a nil in somewhere that you shouldn’t.  Are you using transitions or widget.newTableView?

@rob: yes of course. Here is the entire error:

--------------------------- Corona Runtime Error --------------------------- ?:0: attempt to perform arithmetic on field '?' (a nil value) stack traceback: [C]: ? ?: in function '?' ?: in function \<?:516\> ?: in function \<?:218\> Do you want to relaunch the project? --------------------------- Yes No ---------------------------

However, there are no warnings that appear before this error occurs. And yes, i am using transitions.

What’s happening on lines 516 and 218?

@JonPM: lines 516 and 218 in which file? The error message doesn’t say. Also, i don’t have a line 516 in neither of the scenes i’m  exiting or entering when this error occurs.

I’ve seen this when I had transitions ending, and then the end code would start a new transition (chained using the onComplete basically). The problem in my case was if I exited the screen, but one of the transition would end / fire off a new one (after the screen was removed).

The transitions would compete and try to fire off the new ones, but when the sdk transition code would start trying to manipulate x,y, alphas, etc, that error would pop up (I figured the line numbers were outside of my lua, inside the transition module)…

No idea if something like that is causing it for you, but in my case either making sure to cancel ALL the transition on exiting, or placing some error handling to test if the objects still exist before firing off the next transition seems to have handled it in my case.

Yes thats definitely an issue with transitions. I’ve seen pretty much that exact stack trace quite a few times on different apps recently.  A few other forum topics have popped up with the same issue.

I presume your either doing what mpappas mentioned above, or your using a newScrollView or newTableView before a screen transition. If your doing the latter i find disabling the vertical and horizontal scroll for the widgets just before you move offscreen tends to fix the issue, or at least it did in my case. e.g:

myScrollView.verticalScrollDisabled = true 

myScrollView.horizontalScrollDisabled = true

 

Transitions it is. 

I have disabled (commented) all transitions in several scenes and spent the last three hours clicking around in those scenes. The error never occurred. Although i have always used transition.cancel to cancel transitions and then nil them, the error still occurred. Now i need to find a better way to cancel transitions (maybe double cancel??). 

Many thanks to all of you for your help and suggestions.

Luay

mazrabul - are you using onComplete?

If so, put  a print statement in each completion handler – so you can tell which ones are firing off when they shouldn’t. Also, additional error testing in the completion handlers should help…

In my app, as I load each screen (display group), I call the group ‘this’ (a global for the module)… And in the onComplete routines, I test for ‘this ~= nil’ (I release it / set it to nil when the user exits the screen)… There’s other error tests you can likely do based on your code, but the point is - when the completion handler fires, your onComplete code will need to verify that the code module is still active/ valid (user hasn’t exited screen)… Because if the completion handler tries to access things or fire off another transition after the screen was released- you’ll get invalid access/scoping problems in the sdk.

I wonder how to cancel timers and transitions in a table. What do I have to cancel first?

Example:

transitionStash={}

timerStash={}

This ones are used like this:

startfunction1=function()

     timerStash[#timerStash+1]=timer.performWithDelay(…,startfunction3,…)

end

startfunction2=function()

    transitionStash[#transitionStash+1]=transition.to(…,{…,onComplete=startfunction1})

end

startfunction3=function()

    transitionStash[#transitionStash+1]=transition.to(…,{…,onComplete=startfunction2})

end

My question: Can I still cycle through the tables and cancel the transitions and timers in transitionStash and the timerStash… and what do I have to cancel first? Timers or transitions?

Thx for your help!

Daniela

Hi Daniela,

You can certainly cycle through the tables to cancel these. Just to be safe, I recommend that you cycle through backwards (number of entries back to 1, instead of 1 up to the number of entries).

Also, remember that transitions now have the “tagging” feature which you can use to cancel all by the same tag, so you shouldn’t really need to add these to a table in the first place. Timers, however, don’t have this facility yet, so you’ll need to add those to table.

http://docs.coronalabs.com/api/library/transition/cancel.html

Oh, and I’d recommend that you cancel timers first, but if this is all done in the same routine, it probably won’t matter.

Brent

Thank you for the info Brent!

I used the new transition.cancel() to cancel all running transitions.

I still have a problem here and I wonder if I have to use a “clean” scene between scene-changes to solve it. I have looked through all my timers and I cancel all the timers in scene1 in function scene:hide( event ) inside of phase “will”… but in my scene2 somehow while running code inside of function scene:show( event ) inside of phase “did” I get an error (sometimes) because a value from an image from the first scene1 is not known… something like: object.x …the “x” is nil.

This is strange because the object itself is in the sceneGroup and is getting deleted inside of destroy (in scene1). So where does the operation on the object come from? I have cancelled all timers from the scene and deleted the object but somehow I still get the error from time to time in scene2.

Now I wonder if I HAVE TO USE a “clean” scene between scene changes to make sure everything is cancelled and deleted properly?

I just have build such a clean scene and until know haven’t seen the error anymore… but I don’t feel well not knowing if this REALLY was a solution to such a problem! :frowning:

Do you have any ideas if this maybe was right?

Thank you!

Daniela

Hi Daniela.  Without seeing where you call composer.removeScene(), its very likely that the will phase of the 2nd scene will have fired before the destroy event of scene1 is called. Its very likely that you have some other listener, perhaps a Runtime “enterFrame”, or a physics collision going on if you’ve removed all your timers and transitions. 

Also make sure you’re not using any globals for objects between scenes that could be getting confused.

Rob

I wonder how to cancel timers and transitions in a table. What do I have to cancel first?

Example:

transitionStash={}

timerStash={}

This ones are used like this:

startfunction1=function()

     timerStash[#timerStash+1]=timer.performWithDelay(…,startfunction3,…)

end

startfunction2=function()

    transitionStash[#transitionStash+1]=transition.to(…,{…,onComplete=startfunction1})

end

startfunction3=function()

    transitionStash[#transitionStash+1]=transition.to(…,{…,onComplete=startfunction2})

end

My question: Can I still cycle through the tables and cancel the transitions and timers in transitionStash and the timerStash… and what do I have to cancel first? Timers or transitions?

Thx for your help!

Daniela

Hi Daniela,

You can certainly cycle through the tables to cancel these. Just to be safe, I recommend that you cycle through backwards (number of entries back to 1, instead of 1 up to the number of entries).

Also, remember that transitions now have the “tagging” feature which you can use to cancel all by the same tag, so you shouldn’t really need to add these to a table in the first place. Timers, however, don’t have this facility yet, so you’ll need to add those to table.

http://docs.coronalabs.com/api/library/transition/cancel.html

Oh, and I’d recommend that you cancel timers first, but if this is all done in the same routine, it probably won’t matter.

Brent

Thank you for the info Brent!

I used the new transition.cancel() to cancel all running transitions.

I still have a problem here and I wonder if I have to use a “clean” scene between scene-changes to solve it. I have looked through all my timers and I cancel all the timers in scene1 in function scene:hide( event ) inside of phase “will”… but in my scene2 somehow while running code inside of function scene:show( event ) inside of phase “did” I get an error (sometimes) because a value from an image from the first scene1 is not known… something like: object.x …the “x” is nil.

This is strange because the object itself is in the sceneGroup and is getting deleted inside of destroy (in scene1). So where does the operation on the object come from? I have cancelled all timers from the scene and deleted the object but somehow I still get the error from time to time in scene2.

Now I wonder if I HAVE TO USE a “clean” scene between scene changes to make sure everything is cancelled and deleted properly?

I just have build such a clean scene and until know haven’t seen the error anymore… but I don’t feel well not knowing if this REALLY was a solution to such a problem! :frowning:

Do you have any ideas if this maybe was right?

Thank you!

Daniela

Hi Daniela.  Without seeing where you call composer.removeScene(), its very likely that the will phase of the 2nd scene will have fired before the destroy event of scene1 is called. Its very likely that you have some other listener, perhaps a Runtime “enterFrame”, or a physics collision going on if you’ve removed all your timers and transitions. 

Also make sure you’re not using any globals for objects between scenes that could be getting confused.

Rob