We are developing a Cocoa app for Mac OSX (10.8) which needs to use a JavaScript library (long story why we must use JavaScript).
In a demo application everything seemed to be just fine, but while incorporating the code in our project, we can call the function 37 times without issues and then crashes the 38th time.
To call the JS code we are using Apple's JSWrappers.m (from the JavaScriptCoreHeadStart example). The line that crashes (with a EXC_BAD_ACCESS) is #149:
JSObjectCallAsFunction(self.jsContext, jsFunction, NULL, argumentCount, arguments, NULL);
As stated above, it crashes on the 38th time -callStringJSFunction:withParameters:
is called, no matter what the input is (it crashes with any input string, and the same string works if used in the 37 previous iterations).
The EXC_BAD_ACCESS is not caused by an input variable, as accessing them (such as calling self.jsContext
just immediately before that line) works: it is the function call itself that causes the crash.
We have no idea on what it may be causing this issue, and no idea on how to debug more. Does anyone have any hint? Thanks.
//EDIT
I must correct myself: it doesn't work on the "demo app" too. Even in that case, the code crashes the 38th time we call -callStringJSFunction:withParameters:
//EDIT2
If we re-create the JSWrappers object (and a new JSGlobalContext) every time the function is called, it does not crash anymore. However, this makes the code a lot slower (not surprisingly, as the JS interpreter has to read the script every time - and it's a rather big one).
//EDIT3
Another discovery: building the app in 32 bit makes the code crash. Building in 64 bit, instead, works flawlessly: the JS code is executed without issues any time we wish. This is strange: could this possibly be a bug in the JavaScript Core framework itself?