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

Hi,

While testing my app on the simulator, i get sometimes the following error:

?:0: attempt to perform arithmetic on field ‘?’ (a nil value)

Now, as you can see the error doesn’t point to a specific function or line of code. I read on the forums that such an error message refers to built in corona libraries, and not to the code i wrote my self. That’s fine, but how can i know what is causing the error in the library, especially that, the error doesn’t occur always in the same place.

To clarify, let’s say the error occurs after performing certain steps in the app. When the error occurs the simulator stops with the above error message. If i relaunch the simulator and perform the exact same steps (try to reproduce the error), the error doesn’t occur. However, a few scenes later, the error might re-occur.

Any ideas on how to hunt this error down?

Many thanks

Luay

Usually in my case I have an event listener and the listener is trying to call a function that does not exist.

Could be destroy scene event listener that is trying to do that? 

@jonjonsson: thanks for your reply.

I suspect it might have something to do with destroyScene, but all i’m doing there is disposing of image objects and audio. I am not calling any fuctions.

@jonjonsson: come to think about it, it definitely has something to do with destroyScene as the error always occurs (when it does, that is) when exiting a scene. But why does it occur only sometimes and in different scenes?

Sometimes this error happens if you are calling storyboard.destroyAll() or storyboard.purgeScene() at the wrong time.

And you have to make sure to have paused/stopped all animations and transitions.

@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.

Usually in my case I have an event listener and the listener is trying to call a function that does not exist.

Could be destroy scene event listener that is trying to do that? 

@jonjonsson: thanks for your reply.

I suspect it might have something to do with destroyScene, but all i’m doing there is disposing of image objects and audio. I am not calling any fuctions.

@jonjonsson: come to think about it, it definitely has something to do with destroyScene as the error always occurs (when it does, that is) when exiting a scene. But why does it occur only sometimes and in different scenes?

Sometimes this error happens if you are calling storyboard.destroyAll() or storyboard.purgeScene() at the wrong time.

And you have to make sure to have paused/stopped all animations and transitions.