Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EXC_BAD_ACCESS Crash When Calling Delegate #9

Open
zschuessler opened this issue Feb 9, 2017 · 2 comments
Open

EXC_BAD_ACCESS Crash When Calling Delegate #9

zschuessler opened this issue Feb 9, 2017 · 2 comments
Labels

Comments

@zschuessler
Copy link

zschuessler commented Feb 9, 2017

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!

@matt-curtis
Copy link
Owner

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.

@zschuessler
Copy link
Author

zschuessler commented Feb 16, 2017

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants