MochaJSDelegate icon indicating copy to clipboard operation
MochaJSDelegate copied to clipboard

EXC_BAD_ACCESS Crash When Calling Delegate

Open zschuessler opened this issue 8 years ago • 2 comments

I've created a WebView which responds to a URL change event.

The crash happens most often when calling a delegate multiple times very quickly, but have been able to reproduce when my WebView isn't completely instantiated on first load as well (although rare). If I click a button in my WebView that changes the URL even three times very quickly, I get the crash every time.

Since EXC_BAD_ACCESS commonly relates to referencing an object which no longer exists, I saw this line as suspect in MochaJSDelegate:

var dynamicFunction = eval('(function (' + args.join(', ') + ') { return dynamicHandler.apply(this, arguments); })')

If I replace with this, it works flawlessly:

delegateClassDesc.addInstanceMethodWithSelector_function_(selector, function() {
     func.apply(delegateClassDesc, arguments);
})

Somewhere along the way of using eval and calling the two anonymous functions (see also dynamicHandler function), a disconnect exists with memory management. Note that with my workaround, I'm unable to access the function parameters in my delegate function, so it's not a real solution for the MochaJSDelegate library.

As a result I ended up creating the class delegate manually myself (allowing me to access arguments), but it'd be nice if I could have both arguments and no crashing in the current lib so I don't have to do it manually in my app.

If you have any ideas with that broad overview let me know! I can setup an example if you aren't sure just based on that information where the issue is.

Thanks!

zschuessler avatar Feb 09 '17 02:02 zschuessler

Thanks for the detailed issue! This is interesting, as I've seen similar issues, but been unable to track down the exact point of error. Will look into this.

matt-curtis avatar Feb 10 '17 03:02 matt-curtis

Hey Matt - little update for you! What I listed above about a race condition / memory allocation issue is still valid, but I've tracked down a telling side effect.

In Googling some more about unrelated issues, I noticed a closed issue in this repo about a delegate having the wrong number of parameters crashing the application. Sure enough, I removed some of the parameters off the delegate and I was able to determine that I can only reliably depend on the first two parameters here:

https://developer.apple.com/reference/webkit/webframeloaddelegate/1501451-webview?language=objc

The third parameter, seconds, is unreliable when triggering the delegate many times in short succession.

That can either help track down the issue, or introduce a try/catch to continue gracefully if the parameter doesn't exist :)

zschuessler avatar Feb 16 '17 16:02 zschuessler